Chapter :
240 likes | 400 Vues
Chapter :. STRING. 1. Introduction. String and character processing capabilities Text editors Word processors… Expand from previous chapters Class String and type char Class StringBuilder. Fundamentals of Characters and Strings. Importance of characters Character constants
Chapter :
E N D
Presentation Transcript
Chapter : STRING
1. Introduction • String and character processing capabilities • Text editors • Word processors… • Expand from previous chapters • Class String and type char • Class StringBuilder
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
String Constructors • Class String • Provides eight constructors for initialing strings string file = "C:\\MyFolder\\MySubFolder\\MyFile.txt"; string file = @"C:\MyFolder\MySubFolder\MyFile.txt";
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)
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);
string output; string originalString, string1, string2, string3, string4; char[] characterArray = { 'b', 'i', 'r', 't', 'h', ' ', 'd', 'a', 'y' }; // string initialization originalString = "Welcome to C# programming!"; string1 = originalString; string2 = newstring( characterArray ); string3 = newstring( characterArray, 6, 3 ); string4 = newstring( 'C', 5 ); output = "string1 = " + "\"" + string1 + "\"\n" + "string2 = " + "\"" + string2 + "\"\n" + "string3 = " + "\"" + string3 + "\"\n" + "string4 = " + "\"" + string4 + "\"\n";
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 ( intsourceIndex, array[] destination, intdestinationIndex, intcount ) 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;
StringMethods string string1, output; char[ ] characterArray; string1 = "hello there"; characterArray = newchar[ 10 ]; string1.CopyTo( 0, characterArray, 2, 5 ); output += "\nThe character array is: "; for ( int i = 0 ; i < characterArray.Length; i++ ) output += characterArray[ i ];
Comparing Strings • String comparison • Greater than (1) • Less than (-1) • Equal (0) • Method Equals • Test objects for equality • Return a Boolean • Uses lexicographical comparison
Comparing Strings • Method Equals: test objects for equality • Return a bool value: true if equal • boolString.Equals (String s1, String s2) • bool objectString.Equals (String s) • Method CompareTo: greater than, less than • Return a int value: • intString.Compare (String s1, String s2) • intString.Compare (String s1, String s2, bool ignoreCase) • int objectString.CompareTo (String s)
Comparing Strings • String s1, s2; • S1= ; • S2= ; • S1.CompareTo(s2) car Car hi Return -1: S1 < S2 Return 1: S1 > S2 Return -1: S1 < S2 hello Car Cat
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) • ;
Locating Characters in Strings IndexOfAny method: report the first occurrence int objectString.IndexOfAny (char[] value) int objectString.IndexOfAny (char[] value, int startIndex)
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)
Working With String • Bool MyString.StartsWith( s ) • Bool MyString.EndsWith( s ) • string MyString.Substring ( intstartIndex, intlength ) • MyString.ToLower( ) • MyString.ToUpper( ) • MyString.ToString( ) • MyString.Trim( ) • MyString.TrimEnd( ) • MyString.TrimStart ( )
StartsWith(strA) strA=“Working with String” strB=“w” strA.StartsWith("T") = F strA.StartsWith(strB) = F strA.StartsWith(strB.ToUpper) = T
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
Example • strA=“Working with String” • strB = "w“ strA.Substring(8) = with String strA.Substring(8, 4) = with strA.Substring(strA.IndexOf("S")) = String
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)
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.
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] + “ “; }