Bài giảng C# 2010 - Chapter 6: String and String Manipulations

There are many overload Constructors. I would like to tell you the Constructor below:

String Constructor ( Char [])

Initializes a new instance of the String class to the value indicated by an array of Unicode characters.

 

pptx73 trang | Chuyên mục: Visual C# | Chia sẻ: dkS00TYs | Lượt xem: 1924 | Lượt tải: 5download
Tóm tắt nội dung Bài giảng C# 2010 - Chapter 6: String and String Manipulations, để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
s Example string strFormat; strFormat = string.Format("Tỉ giá {0:d} = {1:c}", DateTime.Today, 22.3); MessageBox.Show(strFormat); String Methods Name public int IndexOf( char value ) Description Reports the index of the first occurrence of the specified Unicode character in this string. The zero-based index position of value if that character is found, or -1 if it is not String Methods Example string str = "Nguyễn Thị Long Lanh Chòng Chành Ánh Tuyết"; int nRet=str.IndexOf('T'); nRet=7 int nRet=str.IndexOf(‘t'); nRet=41 int nRet=str.IndexOf(‘Ă'); nRet=-1 String Methods Name public int IndexOf( string value ) Description Reports the index of the first occurrence of the specified string in this instance. The zero-based index position of value if that string is found, or -1 if it is not. If value is String.Empty, the return value is 0 String Methods Example string str = "Học! Học Nữa ! Học Mãi"; int nRet = str.IndexOf("Học"); nRet =0 nRet = str.IndexOf(“Nữa"); nRet =9 nRet = str.IndexOf(“Dạy"); nRet =-1 String Methods Name public int IndexOf( string value, StringComparison comparisonType) Description Reports the index of the first occurrence of the specified string in the current String object. A parameter specifies the type of search to use for the specified string String Methods Example string strHUI = "Welcome C# 2010"; int nRet = strHUI.IndexOf("WELCOME", 	StringComparison.Ordinal); nRet =-1 int nRet = strHUI.IndexOf("WELCOME", 	StringComparison.OrdinalIgnoreCase); nRet =0 String Methods Name public int IndexOfAny( char[] anyOf ) Description Reports the index of the first occurrence in this instance of any character in a specified array of Unicode characters The zero-based index position where any character in anyOf was found; -1 if no character in anyOf was found String Methods Example string str = "Không! không; có? có?"; int nRet=str.IndexOfAny(new char[] { '>','?',';'}); nRet=12 String Methods Name public string Insert( int startIndex, 	string value ) Description Inserts a specified instance of String at a specified index position in this instance String Methods Example string str = "Không sợ , chỉ sợ không công bằng"; str=str.Insert(9, "thiếu"); string str = "Không sợ thiếu, chỉ sợ không công bằng"; String Methods Name public int LastIndexOf( char value ) Description Reports the index position of the last occurrence of a specified Unicode character within this instance. The zero-based index position of value if that character is found, or -1 if it is not String Methods Example string str = "a , b, c; d; e; f, k"; int nRet=str.LastIndexOf(','); nRet=17 nRet=str.LastIndexOf(‘!'); nRet=-1 String Methods Name public int LastIndexOf(string value) Description Reports the index position of the last occurrence of a specified string within this instance The zero-based index position of value if that string is found, or -1 if it is not. If value is String.Empty , the return value is the last index position in this instance String Methods Example string str = "Học ! Học Nữa ! Học Mãi"; int nRet = str.LastIndexOf("Học"); nRet =16 nRet = str.LastIndexOf("HỌC"); nRet =-1 String Methods Name public int LastIndexOf( string value, StringComparison comparisonType ) Description Reports the index of the last occurrence of a specified string within the current String object. A parameter specifies the type of search to use for the specified string. String Methods Example string str = "Học ! Học Nữa ! Học Mãi"; int nRet = str.LastIndexOf("HỌC", StringComparison.OrdinalIgnoreCase); nRet =16 String Methods Name public string Remove( int startIndex ) Description Deletes all the characters from this string beginning at a specified position and continuing through the last position String Methods Example string str = "không có gì quý hơn độc lập tự do"; str=str.Remove(11);  str = "không có gì" String Methods Name public string Remove( int startIndex, 	int count ) Description Deletes a specified number of characters from this instance beginning at a specified position. String Methods Example string str = "Con đường học vấn giúp ta thoát khỏi nghèo khổ nhanh nhất"; str=str.Remove(0,9);  str = "học vấn giúp ta thoát khỏi nghèo khổ nhanh nhất"; String Methods Name public string Replace( char oldChar, 	char newChar ) Description Returns a new string in which all occurrences of a specified Unicode character in this instance are replaced with another specified Unicode character. String Methods Example string str = "Chúc các em học tốt"; str = str.Replace(' ','#'); str = "Chúc#các#em#học#tốt"; String Methods Name public string Replace( string oldValue, 	string newValue ) Description Returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string String Methods Example string str = "Phải xem tài liệu trước khi tới lớp, tài liệu này rất quan trọng!"; str = str.Replace("tài liệu","document"); str = "Phải xem document trước khi tới lớp, document này rất quan trọng!"; String Methods Name public string[] Split( params char[] separator ) Description Returns a string array that contains the substrings in this instance that are delimited by elements of a specified Unicode character array String Methods Example string str = "Trần văn tèo;21 tuổi;Chưa vợ;Côn Đảo"; string []strArr=str.Split(new char[]{';'}); foreach (string s in strArr) { 	Console.WriteLine(s); } You can process strArr[0], strArr[1],…, strArr.Length String Methods Name public string[] Split( string[] separator, StringSplitOptions options ) Description Returns a string array that contains the substrings in this string that are delimited by elements of a specified string array. A parameter specifies whether to return empty array elements String Methods Example string str = "Trần văn tèo;;21 tuổi;Chưa vợ;Côn Đảo"; string []strArr=str.Split(new string[] { ";" }, StringSplitOptions.None); MessageBox.Show(strArr.Length+"");//5 foreach (string s in strArr) { 	MessageBox.Show(s); } //include Empty string String Methods Example string str = "Trần văn tèo;;21 tuổi;Chưa vợ;Côn Đảo"; string []strArr=str.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries); MessageBox.Show(strArr.Length+"");//4 foreach (string s in strArr) { 	MessageBox.Show(s); } //Remove Empty string String Methods Name public bool StartsWith( string value ) Description Determines whether the beginning of this string instance matches the specified string String Methods Example string str = "Học lập trình phải luyện công "; bool bStart=str.StartsWith(""); MessageBox.Show(bStart.ToString()); True bStart=str.StartsWith(""); MessageBox.Show(bStart.ToString()); False String Methods Name public bool StartsWith( string value, StringComparison comparisonType ) Description Determines whether the beginning of this string instance matches the specified string when compared using the specified comparison option String Methods Example string str = "Học lập trình phải luyện công"; bool bStart=str.StartsWith("", StringComparison.Ordinal); MessageBox.Show(bStart.ToString());False bool bStart=str.StartsWith("", StringComparison.OrdinalIgnoreCase); True String Methods Name public string Substring( int startIndex ) Description Retrieves a substring from this instance. The substring starts at a specified character position. String Methods Example string str = "Thay đổi để tồn tại"; string strSub = str.Substring(5); MessageBox.Show(strSub); đổi để tồn tại String Methods Name public string Substring( int startIndex, int length ) Description Retrieves a substring from this instance. The substring starts at a specified character position and has a specified length. String Methods Example string str = "Microsoft Visual Studio 2010"; string strSub = str.Substring(10,13); MessageBox.Show(strSub); Visual Studio strSub = str.Substring(0,9); Microsoft String Methods Name public char[] ToCharArray() Description Copies the characters in this instance to a Unicode character array. String Methods Example string str = "Microsoft Visual Studio 2010"; char []chrArr=str.ToArray(); MessageBox.Show(chrArr.Length+"");//28 foreach (char c in chrArr) { 	MessageBox.Show(c.ToString()); } String Methods Name public string ToLower() Description Returns a copy of this string converted to lowercase. String Methods Example string str = "Microsoft Visual Studio 2010"; str=str.ToLower(); MessageBox.Show(str); microsoft visual studio 2010 String Methods Name public string ToUpper() Description Returns a copy of this string converted to uppercase. String Methods Example string str = "Microsoft Visual Studio 2010"; str=str.ToUpper(); MessageBox.Show(str); MICROSOFT VISUAL STUDIO 2010 String Methods Name public string Trim() Description Removes all leading and trailing white-space characters from the current String object. String Methods Example string str = " Microsoft Visual Studio 2010 "; MessageBox.Show(str.Length+"");//35 MessageBox.Show(str); " Microsoft Visual Studio 2010 " string str = " Microsoft Visual Studio 2010 "; str=str.Trim(); MessageBox.Show(str.Length+""); //28 MessageBox.Show(str); str = "Microsoft Visual Studio 2010"; String Methods Name public string TrimEnd( params char[] trimChars ) Description Removes all trailing occurrences of a set of characters specified in an array from the current String object. String Methods Example string str = "Microsoft Visual Studio 2010 #?! #"; str=str.TrimEnd(new char[] { ' ','?','!','#' }); MessageBox.Show(str.Length+"");//28 MessageBox.Show(str); "Microsoft Visual Studio 2010" String Methods Name public string TrimStart( params char[] trimChars ) Description Removes all leading occurrences of a set of characters specified in an array from the current String object. String Methods Example string str = " #?! #Microsoft Visual Studio 2010"; str=str.TrimStart(new char[] { ' ','?','!','#' }); MessageBox.Show(str.Length+"");//28 MessageBox.Show(str); "Microsoft Visual Studio 2010" DEMO String & Dictionary END 

File đính kèm:

  • pptxBài giảng C# 2010 - Chapter 6 String and String Manipulations.pptx
Tài liệu liên quan