1 / 24

Chapter :

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

zuwena
Télécharger la présentation

Chapter :

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter : STRING

  2. 1. Introduction • String and character processing capabilities • Text editors • Word processors… • Expand from previous chapters • Class String and type char • Class StringBuilder

  3. 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

  4. String Constructors • Class String • Provides eight constructors for initialing strings string file = "C:\\MyFolder\\MySubFolder\\MyFile.txt"; string file = @"C:\MyFolder\MySubFolder\MyFile.txt";

  5. 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)

  6. 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);

  7. 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";

  8. StringConstructor.cs

  9. 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;

  10. 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 ];

  11. Comparing Strings • String comparison • Greater than (1) • Less than (-1) • Equal (0) • Method Equals • Test objects for equality • Return a Boolean • Uses lexicographical comparison

  12. 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)

  13. 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

  14. 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) • ;

  15. Locating Characters in Strings IndexOfAny method: report the first occurrence int objectString.IndexOfAny (char[] value) int objectString.IndexOfAny (char[] value, int startIndex)

  16. 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)

  17. 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 ( )

  18. StartsWith(strA) strA=“Working with String” strB=“w” strA.StartsWith("T") = F strA.StartsWith(strB) = F strA.StartsWith(strB.ToUpper) = T

  19. 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

  20. Example • strA=“Working with String” • strB = "w“ strA.Substring(8) = with String strA.Substring(8, 4) = with strA.Substring(strA.IndexOf("S")) = String

  21. 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)

  22. 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.

  23. 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] + “ “; }

  24. Finish

More Related