1. Trang chủ
  2. » Giáo án - Bài giảng

Chapter String Introdution

24 104 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 24
Dung lượng 201,5 KB

Nội dung

Chapter : STRING 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 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 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"; 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) 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); 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"; StringCons tructor.cs 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 ) 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 = 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 [...]... objectString.LastIndexOf (char[] value, int startIndex) 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 ( ) StartsWith(strA) strA=“Working with String strB=“w” strA.StartsWith("T") =F strA.StartsWith(strB)... value: • int String. Compare (String s1, String s2) • int String. Compare (String s1, String s2, bool ignoreCase) • int objectString.CompareTo (String s) Comparing Strings • • • • String s1, s2; hi car S1= Car ; Return -1: S1 < S2 < 1: Return -1: S1 > S2 Car Cat S2= hello ; S1.CompareTo(s2) Locating Characters in Strings • IndexOf methods: Reports the index of the first occurrence of a String, or one... 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... 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. ..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 • bool String. Equals (String s1, String s2) • bool objectString.Equals (String s) • Method CompareTo:... 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... 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... 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 . char – Class StringBuilder Slide 3 9:06: 37 AM Fundamentals of Characters and Strings • Importance of characters – Character constants • Character code • Unicode character set – String • Object. type Char (System namespace) – Class StringBuilder (System.Text namespace) Slide 7 9:06: 37 AM String class: Initialing strings Using constructors of String class: new String (char[ ] chuoi_ki_tu); new. of class String in System namespace • Consist of characters Slide 4 9:06: 37 AM String Constructors • Class String – Provides eight constructors for initialing strings string file = " ;C: \MyFolder\MySubFolder\MyFile.txt"; string

Ngày đăng: 13/05/2014, 11:39