+ All Categories
Home > Documents > Chapter : STRING. Slide 2 9:06:37 AM 1. Introduction String and character processing capabilities...

Chapter : STRING. Slide 2 9:06:37 AM 1. Introduction String and character processing capabilities...

Date post: 01-Jan-2016
Category:
Upload: wilfrid-wilkinson
View: 217 times
Download: 2 times
Share this document with a friend
Popular Tags:
24
Chapter : STRING
Transcript
Page 1: Chapter : STRING. Slide 2 9:06:37 AM 1. Introduction String and character processing capabilities –Text editors –Word processors… Expand from previous.

Chapter :

STRING

Page 2: Chapter : STRING. Slide 2 9:06:37 AM 1. Introduction String and character processing capabilities –Text editors –Word processors… Expand from previous.

Slide 29:06:37 AM

1. Introduction

• String and character processing capabilities– Text editors

– Word processors…

• Expand from previous chapters– Class String and type char

– Class StringBuilder

Page 3: Chapter : STRING. Slide 2 9:06:37 AM 1. Introduction String and character processing capabilities –Text editors –Word processors… Expand from previous.

Slide 39:06:37 AM

Fundamentals of Characters and Strings

• Importance of characters– Character constants

• Character code

• Unicode character set

– String• Object of class String in System namespace

• Consist of characters

Page 4: Chapter : STRING. Slide 2 9:06:37 AM 1. Introduction String and character processing capabilities –Text editors –Word processors… Expand from previous.

Slide 49:06:37 AM

String Constructors

• Class String– Provides eight constructors for initialing strings

string file = "C:\\MyFolder\\MySubFolder\\MyFile.txt";

string file = @"C:\MyFolder\MySubFolder\MyFile.txt";

Page 5: Chapter : STRING. Slide 2 9:06:37 AM 1. Introduction String and character processing capabilities –Text editors –Word processors… Expand from previous.

Slide 59:06:37 AM

Introduction

• String and character processing capabilities– Text editors

– Word processors…

• Expand from previous chapters– Class String and type Char (System namespace)

– Class StringBuilder (System.Text namespace)

Page 6: Chapter : STRING. Slide 2 9:06:37 AM 1. Introduction String and character processing capabilities –Text editors –Word processors… Expand from previous.

Slide 79:06:37 AM

String class: Initialing strings

Using constructors of String class:

new String (char[ ] chuoi_ki_tu);

new String (char[ ] chuoi_ki_tu, int vi_tri_bat_dau_lay, int so_ki_tu);

new String (char ki_tu, int so_lan_lap);

Page 7: Chapter : STRING. Slide 2 9:06:37 AM 1. Introduction String and character processing capabilities –Text editors –Word processors… Expand from previous.

string output;string originalString, string1, string2, string3, string4; char[] characterArray = { 'b', 'i', 'r', 't', 'h', ' ', 'd', 'a', 'y' }; // string initializationoriginalString = "Welcome to C# programming!";string1 = originalString;string2 = new string( characterArray );string3 = new string( characterArray, 6, 3 );string4 = new string( 'C', 5 ); output = "string1 = " + "\"" + string1 + "\"\n" +

"string2 = " + "\"" + string2 + "\"\n" + "string3 = " + "\"" + string3 + "\"\n" + "string4 = " + "\"" + string4 + "\"\n";

Page 8: Chapter : STRING. Slide 2 9:06:37 AM 1. Introduction String and character processing capabilities –Text editors –Word processors… Expand from previous.

StringConstructor.cs

Page 9: Chapter : STRING. Slide 2 9:06:37 AM 1. Introduction String and character processing capabilities –Text editors –Word processors… Expand from previous.

Slide 109:06:37 AM

String Length Property and CopyTo Method

• Length property – Returns the length of the string

• CopyTo– Copies specified number of characters into a char

array

CopyTo ( int sourceIndex, array[] destination, int destinationIndex, int count )

Copy To: tại 1 ví trí trên chuỗi gốc (sourceIndex, ), lấy một số ký tự (count), Copy tới một vị trí (destinationIndex) , trên một bảng dãy ký tự Unicode destination;

Page 10: Chapter : STRING. Slide 2 9:06:37 AM 1. Introduction String and character processing capabilities –Text editors –Word processors… Expand from previous.

StringMethodsstring string1, output;char[ ] characterArray; string1 = "hello there";characterArray = new char[ 10 ];

string1.CopyTo( 0, characterArray, 2, 5 );output += "\nThe character array is: ";

for ( int i = 0 ; i < characterArray.Length; i++ )output += characterArray[ i ];

h e l l o

Page 11: Chapter : STRING. Slide 2 9:06:37 AM 1. Introduction String and character processing capabilities –Text editors –Word processors… Expand from previous.

Slide 129:06:37 AM

Comparing Strings

• String comparison– Greater than (1)

– Less than (-1)

– Equal (0)

• Method Equals– Test objects for equality

– Return a Boolean

– Uses lexicographical comparison

Page 12: Chapter : STRING. Slide 2 9:06:37 AM 1. Introduction String and character processing capabilities –Text editors –Word processors… Expand from previous.

Slide 139:06:37 AM

Comparing Strings

• Method Equals: test objects for equality

– Return a bool value: true if equal• bool String.Equals (String s1, String s2)

• bool objectString.Equals (String s)

• Method CompareTo: greater than, less than

– Return a int value:• int String.Compare (String s1, String s2)

• int String.Compare (String s1, String s2, bool ignoreCase)

• int objectString.CompareTo (String s)

Page 13: Chapter : STRING. Slide 2 9:06:37 AM 1. Introduction String and character processing capabilities –Text editors –Word processors… Expand from previous.

Slide 149:06:37 AM

Comparing Strings

• String s1, s2;

• S1= ;

• S2= ;

• S1.CompareTo(s2)

hello

hiReturn 1: S1 > S2

car

CarReturn -1: S1 < S2

Cat

CarReturn -1: S1 < S2

Page 14: Chapter : STRING. Slide 2 9:06:37 AM 1. Introduction String and character processing capabilities –Text editors –Word processors… Expand from previous.

Slide 159:06:37 AM

Locating Characters in Strings

• IndexOf methods: Reports the index of the first occurrence of a String, or one or more characters, within this string.

– IndexOf(char value)

– IndexOf(char value, int StartIndex)

– IndexOf(string value)

– IndexOf(string value, int StartIndex)

– ;

Page 15: Chapter : STRING. Slide 2 9:06:37 AM 1. Introduction String and character processing capabilities –Text editors –Word processors… Expand from previous.

Slide 169:06:37 AM

Locating Characters in Strings

IndexOfAny method: report the first occurrence

int objectString.IndexOfAny (char[] value)

int objectString.IndexOfAny (char[] value, int startIndex)

Page 16: Chapter : STRING. Slide 2 9:06:37 AM 1. Introduction String and character processing capabilities –Text editors –Word processors… Expand from previous.

Slide 179:06:37 AM

Locating Characters in Strings

– LastIndexOf method: report the last occurence• int objectString.LastIndexOf (string value)• int objectString.LastIndexOf (string value, int startIndex)• int objectString.LastIndexOf (string value, int startIndex, int

limit)• int objectString.LastIndexOf (char value)• int objectString.LastIndexOf (char value, int startIndex)

– LastIndexOfAny method: report the last occurrence

• int objectString.LastIndexOf (char[] value)

• int objectString.LastIndexOf (char[] value, int startIndex)

Page 17: Chapter : STRING. Slide 2 9:06:37 AM 1. Introduction String and character processing capabilities –Text editors –Word processors… Expand from previous.

Slide 189:06:37 AM

Working With String

1. Bool MyString.StartsWith( s )

2. Bool MyString.EndsWith( s )

3. string MyString.Substring ( int startIndex, int length )

4. MyString.ToLower( )

5. MyString.ToUpper( )

6. MyString.ToString( )

7. MyString.Trim( )

8. MyString.TrimEnd( )

9. MyString.TrimStart ( )

Page 18: Chapter : STRING. Slide 2 9:06:37 AM 1. Introduction String and character processing capabilities –Text editors –Word processors… Expand from previous.

Slide 199:06:37 AM

StartsWith(strA)

strA=“Working with String”

strB=“w”strA.StartsWith("T") = F

strA.StartsWith(strB) = F

strA.StartsWith(strB.ToUpper) = T

Page 19: Chapter : STRING. Slide 2 9:06:37 AM 1. Introduction String and character processing capabilities –Text editors –Word processors… Expand from previous.

Slide 209:06:37 AM

Example

• strA=“Working with String”

• strB = "w“

strA.IndexOf("w") = 8

strA.IndexOf(strB.ToUpper) = 0

strA.IndexOf(strB.ToUpper, 3) = -1

strA.IndexOf(“ “, 3) = 7

Page 20: Chapter : STRING. Slide 2 9:06:37 AM 1. Introduction String and character processing capabilities –Text editors –Word processors… Expand from previous.

Slide 219:06:37 AM

Example

• strA=“Working with String”• strB = "w“

strA.Substring(8) = with String

strA.Substring(8, 4) = with

strA.Substring(strA.IndexOf("S")) = String

Page 21: Chapter : STRING. Slide 2 9:06:37 AM 1. Introduction String and character processing capabilities –Text editors –Word processors… Expand from previous.

Slide 229:06:37 AM

Replace String Methods

• Method Replace (phương thức thay thế chuỗi)– Original string remain unchanged

– String objectString.Replace (String oldValue, String newValue)

– String objectString.Replace (char oldValue, char newValue)

Page 22: Chapter : STRING. Slide 2 9:06:37 AM 1. Introduction String and character processing capabilities –Text editors –Word processors… Expand from previous.

Slide 239:06:37 AM

Tách chuỗi

• Sử dụng phương thức Split() của lớp String: phân tích một chuỗi ra thành các chuỗi con dựa vào ký tự phân cách.

• Cú pháp: Split (char[] sign), hàm trả về một mảng chuỗi

• Để sử dụng Split(), chúng ta truyền vào một mảng các ký tự phân cách, các ký tự này được dùng để chia các từ trong chuỗi.

• Ví dụ: Tách chuỗi “Nguyễn Văn An” dựa vào khoảng trắng.

Page 23: Chapter : STRING. Slide 2 9:06:37 AM 1. Introduction String and character processing capabilities –Text editors –Word processors… Expand from previous.

Slide 249:06:37 AM

Ví dụ

string s1 = “Nguyễn Văn An";char[ ] space = {' '};string[ ] s;

s = s1.Split (space); //tham so truyen la mang ky tu

txtHo.Text = s[0];txtTen.Text = s[s.Length - 1];for ( int i=1; i<s.Length-2; i++){

txtHolot.Text += s[i] + “ “;}

Nguyễn

Văn

An

Page 24: Chapter : STRING. Slide 2 9:06:37 AM 1. Introduction String and character processing capabilities –Text editors –Word processors… Expand from previous.

Slide 259:06:37 AM

Finish


Recommended