DONG NAI UNIVERSITY OF TECHNOLOGYString Properties specified character position in the current String object.. DONG NAI UNIVERSITY OF TECHNOLOGYString Methods Name public bool Contains s
Trang 1DONG NAI UNIVERSITY OF TECHNOLOGY
Trang 2Represents text as a series
of Unicode characters.
Trang 3DONG NAI UNIVERSITY OF TECHNOLOGY
Constructor
There are many overload Constructors I would like to tell you the Constructor below:
String Constructor ( Char [])
class to the value indicated by an array
of Unicode characters
Trang 4(new char[]{'H','U','I'});
MessageBox.Show(strHUI);
String strHUI = new String
MessageBox.Show(strHUI);
Use string or String ?
Trang 5DONG NAI UNIVERSITY OF TECHNOLOGY
Trang 6“a”==“A”?false
“a”==“a”?true
Determines whether two specified strings have the same value
Inequality(!=)
“a”!=“A”?true
“a”!=“a”?false
Determines whether two specified strings have different values
Trang 7DONG NAI UNIVERSITY OF TECHNOLOGY
String Properties
specified character position
in the current String object
characters in the current String object
Trang 8( string strA, string strB )Description
and returns an integer that indicates
their relative position in the sort order
0 strA equals strB
-1 strA is less than strB
Trang 9DONG NAI UNIVERSITY OF TECHNOLOGY
Trang 10string strA, string strB,
bool ignoreCase )
Description
,ignoring or honoring their 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 12Compares this instance with a
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 14string str = string.Copy("Teo");
string str1 = "Ti";
string str2 =string.Copy(str1);
Trang 15DONG NAI UNIVERSITY OF TECHNOLOGY
String Methods
Name
public bool Contains( string value )
Description
Returns a value indicating whether the
this string
Return true or false
Trang 16string strA = "Trần Văn Tèo";
Trang 17DONG NAI UNIVERSITY OF TECHNOLOGY
String Methods
Name
public bool EndsWith( string value )
Description
Determines whether the end of this
string instance matches the specified
string
Return true or false
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 24occurrence of the specified Unicode
character in this string
of value if that character is found, or -1
if it is not
Trang 25DONG NAI UNIVERSITY OF TECHNOLOGY
Trang 26occurrence 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
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 28StringComparison 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
Trang 29DONG NAI UNIVERSITY OF TECHNOLOGY
String Methods
Example
string strHUI = "Welcome C# 2010" ;
int nRet = strHUI.IndexOf( "WELCOME" ,
Trang 30Reports 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
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
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
The zero-based index position
of value if that character is found, or -1
if it is not
Trang 35DONG NAI UNIVERSITY OF TECHNOLOGY
Trang 36Reports 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.LastIndexOf("HỌC");
Trang 38StringComparison 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
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 40Deletes 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
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
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 48separator )
Description
Returns 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 50StringSplitOptions 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 [] { ";" },
Trang 52string str = "Trần văn tèo;;21 tuổi;Chưa vợ;Côn Đảo" ;
string []strArr=str.Split( new string [] { ";" },
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 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 56công</head>" ;
bool 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
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
String Methods
Name
public char[] ToCharArray()
Description
Copies the characters in this instance
to a Unicode character array
Trang 63DONG NAI UNIVERSITY OF TECHNOLOGY
Trang 64MessageBox Show(str);
microsoft visual studio 2010
Trang 65DONG NAI UNIVERSITY OF TECHNOLOGY
Trang 66MessageBox Show(str);
MICROSOFT VISUAL STUDIO 2010
Trang 67DONG NAI UNIVERSITY OF TECHNOLOGY
String Methods
Name
public string Trim()
Description
Removes all leading and trailing
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
Removes all trailing occurrences of a
set of characters specified in an array
Trang 70str=str.TrimEnd(new char [] { ' ' , '?' , '!' , '#' });
MessageBox Show(str.Length+ "" );// 28
MessageBox Show(str);
"Microsoft Visual Studio 2010"
Trang 71DONG NAI UNIVERSITY OF TECHNOLOGY
Removes all leading occurrences of a
set of characters specified in an array
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
Trang 74END