Download presentation
Presentation is loading. Please wait.
Published byWilfrid Wilkinson Modified over 9 years ago
1
Chapter : STRING
2
Slide 2 9: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
3
Slide 3 9: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
4
Slide 4 9: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";
5
Slide 5 9: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)
6
Slide 6 9:06:37 AM Fundamentals of Characters and Strings Importance of characters –Character constants Character code (ex. 122 ‘z’, 10 ’\n’) Unicode character set (see Appendix G, Unicode) –String Consist of characters Object of class String in System namespace using String to refer to the class String and string to refer to an object of class String
7
Slide 7 9: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);
8
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 = 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";
9
StringCons tructor.cs
10
Slide 10 9: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 )int 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;
11
StringMethods string 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 ]; hello
12
Slide 12 9: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
13
Slide 13 9: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)
14
Slide 14 9:06:37 AM Comparing Strings String s1, s2; S1= ; S2= ; S1.CompareTo(s2) hello hi Return 1: S1 > S2 car Car Return -1: S1 < S2 Cat Car Return -1: S1 < S2
15
Slide 15 9: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.String –IndexOf(char value) –IndexOf(char value, int StartIndex) –IndexOf(string value) –IndexOf(string value, int StartIndex) –;–;
16
Slide 16 9: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)
17
Slide 17 9: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)
18
Slide 18 9:06:37 AM Working With String 1.Bool MyString.StartsWith( s ) 2.Bool MyString.EndsWith( s ) 3.string MyString.Substring ( int startIndex, int length )stringint 4.MyString.ToLower( ) 5.MyString.ToUpper( ) 6.MyString.ToString( ) 7.MyString.Trim( ) 8.MyString.TrimEnd( ) 9.MyString.TrimStart ( )
19
Slide 19 9:06:37 AM StartsWith(strA) strA=“Working with String” strB=“w” strA.StartsWith("T") = F strA.StartsWith(strB) = F strA.StartsWith(strB.ToUpper) = T
20
Slide 20 9: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
21
Slide 21 9: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
22
Slide 22 9: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)
23
Slide 23 9: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.
24
Slide 24 9: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
25
Slide 25 9:06:37 AM Finish
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.