Bài giảng Lập trình C# 2010: Chương 6 - String Class có nội dung trình bày về: Constructor, String Field, String Operators, String Methods, DEMO String & Dictionary.
Trang 1DONG NAI UNIVERSITY OF TECHNOLOGY
Trang 2characters.
Trang 3DONG NAI UNIVERSITY OF TECHNOLOGY
Constructor
There are many overload Constructors I would like to tell you the Constructor below:
String Constructor ( Char [])
an array of Unicode characters
Trang 4(new char[]{'H ','U ','I'});
M essageBox.Show (strH U I);
String strH U I = new String
(new char[]{'H ','U ','I'});
M essageBox.Show (strH U I);
Use string or String?
Use string or String?
Trang 5DONG NAI UNIVERSITY OF TECHNOLOGY
Trang 7DONG NAI UNIVERSITY OF TECHNOLOGY
String Properties
Chars Gets the character at a specified character
position in the current String object
Length Gets the number of characters in the
current String object
Trang 8( string strA, string strB )Description
indicates their relative position in the sort order
-1 strA is less than strB
Trang 9DONG NAI UNIVERSITY OF TECHNOLOGY
Trang 10string strA, string strB,
bool ignoreCase )
Description
case, and returns an integer that indicates their relative position in
the sort order
Trang 11DONG NAI UNIVERSITY OF TECHNOLOGY
String Methods
Example
(“A", “a”,true);nRet=0
(“a", “A”,false);nRet=-1
(“A", “a”,false);nRet=1
Trang 12public int CompareTo( string strB )
Description
whether this instance precedes, follows, or appears in the same
position in the sort order as the specified String (0 ; 1 ; -1)
Trang 13DONG NAI UNIVERSITY OF TECHNOLOGY
Trang 15DONG NAI UNIVERSITY OF TECHNOLOGY
String Methods
Name
public bool Contains( string value )
Description
occurs within this string
Return true or false
Trang 17DONG NAI UNIVERSITY OF TECHNOLOGY
Trang 19DONG NAI UNIVERSITY OF TECHNOLOGY
Determines whether the end of this string instance matches the
specified string when compared using the specified comparison
option
Trang 21DONG NAI UNIVERSITY OF TECHNOLOGY
String Methods
Name
public static string Format
(string format, params Object[] args )
Description
Replaces the format item in a specified string with the string
representation of a corresponding object in a specified array
Trang 22strFormat = string.Format("Tỉ giá {0:d} = {1:c}", DateTime.Today,
22.3);
MessageBox.Show(strFormat);
Trang 23HO CHI MINH UNIVERSITY OF INDUSTRY
Author: Duy Thanh Tran – Phone : 0987773061 - Email: thanhxeiko@gmail.com – Blog: http://thanhxeiko.wordpress.com
Trang 24character in this string
or -1 if it is not
Trang 25DONG NAI UNIVERSITY OF TECHNOLOGY
String Methods
Example
string str = "N guyễễ n Thị Long Lanh Chòng Chành Ánh Tuyễế t" ;
int nRet= str.IndexO f( 'T' );
Trang 26public int IndexOf( string value )
Description
this instance
Trang 27DONG NAI UNIVERSITY OF TECHNOLOGY
String Methods
Example
string str = "Học! Học Nữa ! Học Mãi" ;
int nRet = str.IndexOf( "Học" );
Trang 28Description
Reports the index of the first occurrence of the specified string in
to use for the specified string
Trang 29DONG NAI UNIVERSITY OF TECHNOLOGY
String Methods
Example
string strHUI = "Welcome C# 2010" ;
int nRet = strHUI.IndexOf( "WELCOME" ,
Trang 30public 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
Trang 31DONG NAI UNIVERSITY OF TECHNOLOGY
String Methods
Example
string str = "Không! không; có? có?" ;
int nRet=str.IndexOfAny( new char [] { '>' , '?' , ';' });
nRet=12
Trang 32string value )Description
Inserts a specified instance of String at a specified index position
in this instance
Trang 33DONG NAI UNIVERSITY OF TECHNOLOGY
Trang 34Reports the index position of the last occurrence of a specified
Unicode character within this instance
-1 if it is not
Trang 35DONG NAI UNIVERSITY OF TECHNOLOGY
Trang 36public 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
Trang 37DONG NAI UNIVERSITY OF TECHNOLOGY
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.LastIndexO f("H Ọ C");
nRet = -1
Trang 38comparisonType )
Description
Reports the index of the last occurrence of a specified string within
to use for the specified string
Trang 39DONG NAI UNIVERSITY OF TECHNOLOGY
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
Trang 40public string Remove( int startIndex )
Description
Deletes all the characters from this string beginning at a specified
position and continuing through the last position
Trang 41DONG NAI UNIVERSITY OF TECHNOLOGY
Trang 42int count )Description
Deletes a specified number of characters from this instance
beginning at a specified position
Trang 43DONG NAI UNIVERSITY OF TECHNOLOGY
Trang 44char 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
Trang 45DONG NAI UNIVERSITY OF TECHNOLOGY
Trang 46string newValue )
Description
Returns a new string in which all occurrences of a specified string
in the current instance are replaced with another specified string
Trang 47DONG NAI UNIVERSITY OF TECHNOLOGY
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!" ;
Trang 48Returns a string array that contains the substrings in this instance
that are delimited by elements of a specified Unicode character
array
Trang 49DONG NAI UNIVERSITY OF TECHNOLOGY
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)
Trang 50public 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
Trang 51DONG NAI UNIVERSITY OF TECHNOLOGY
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);
M essageBox Show (strArr.Length+ "" );// 5
foreach ( string s in strArr)
Trang 52string []strArr=str.Split( new string [] { ";" }, StringSplitOptions.R em oveEm ptyEntries);
M essageBox Show (strArr.Length+ "" );// 4
foreach ( string s in strArr)
Trang 53DONG NAI UNIVERSITY OF TECHNOLOGY
String Methods
Name
public bool StartsWith( string value )
Description
Determines whether the beginning of this string instance matches
the specified string
Trang 54bool bStart=str.StartsWith( "<head>" );
Trang 55DONG NAI UNIVERSITY OF TECHNOLOGY
Determines whether the beginning of this string instance matches
the specified string when compared using the specified
comparison option
Trang 56bool bStart=str.StartsWith( "<HEAD>" ,
StringComparison Ordinal);
MessageBox Show(bStart.ToString());False
bool bStart=str.StartsWith( "<HEAD>" ,
StringComparison OrdinalIgnoreCase);
True
Trang 57DONG NAI UNIVERSITY OF TECHNOLOGY
String Methods
Name
public string Substring( int startIndex )
Description
Retrieves a substring from this instance The substring starts at a
specified character position
Trang 58string strSub = str.Substring(5);
MessageBox Show(strSub);
Trang 59DONG NAI UNIVERSITY OF TECHNOLOGY
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
Trang 60string strSub = str.Substring(10,13);
MessageBox Show(strSub);
strSub = str.Substring(0,9);
Trang 61DONG NAI UNIVERSITY OF TECHNOLOGY
Trang 63DONG NAI UNIVERSITY OF TECHNOLOGY
Trang 64MessageBox Show(str);
microsoft visual studio 2010
Trang 65DONG NAI UNIVERSITY OF TECHNOLOGY
Trang 66MessageBox Show(str);
M ICRO SO FT VISU AL STU D IO 2010
Trang 67DONG NAI UNIVERSITY OF TECHNOLOGY
String Methods
Name
public string Trim()
Description
Removes all leading and trailing white-space characters from the
current String object
Trang 68MessageBox Show(str.Length+ "" ); //35
MessageBox Show(str);
" Microsoft Visual Studio 2010 "
string str = " Microsoft Visual Studio 2010 " ;
Trang 69DONG NAI UNIVERSITY OF TECHNOLOGY
String Methods
Name
public string TrimEnd( params char[] trimChars )
Description
Removes all trailing occurrences of a set of characters specified in
Trang 70str=str.TrimEnd(new char [] { ' ' , '?' , '!' , '#' });
MessageBox Show(str.Length+ "" );// 28
MessageBox Show(str);
"Microsoft Visual Studio 2010"
Trang 71DONG NAI UNIVERSITY OF TECHNOLOGY
String Methods
Name
public string TrimStart( params char[] trimChars )
Description
Removes all leading occurrences of a set of characters specified in
Trang 72str=str.TrimStart(new char [] { ' ' , '?' , '!' , '#' });
MessageBox Show(str.Length+ "" );// 28
MessageBox Show(str);
"Microsoft Visual Studio 2010"
Trang 73DONG NAI UNIVERSITY OF TECHNOLOGY
DEMO String & Dictionary
DEMO String & Dictionary
Trang 74END