DONG NAI UNIVERSITY OF TECHNOLOGY
DONG NAI UNIVERSITY OF TECHNOLOGY
String Class
Represents text as a series of Unicode
characters.
DONG NAI UNIVERSITY OF TECHNOLOGY
Constructor
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.
DONG NAI UNIVERSITY OF TECHNOLOGY
Constructor
strin g strH U I= new strin g
(new char[]{'H ','U ','I'});
M essageBox.Show (strH U I);
Use string or S trin g ?
S trin g strH U I= new S trin g
(new char[]{'H ','U ','I'});
M essageBox.Show (strH U I);
DONG NAI UNIVERSITY OF TECHNOLOGY
String Fields
Empty
Represents the empty string. This field is readonly.
string strName = string.Empty;
DONG NAI UNIVERSITY OF TECHNOLOGY
String Operators
Name
Description
Equality(==)
Determines whether two specified
“a”==“A”?false
strings have the same value.
“a”==“a”?true
Inequality(!=)
Determines whether two specified
“a”!=“A”?true
strings have different values.
“a”!=“a”?false
DONG NAI UNIVERSITY OF TECHNOLOGY
String Properties
Name
Description
Chars
Gets the character at a specified character
position in the current String object.
char ch = strHUI[0];
Length
Gets the number of characters in the
current String object.
int nSize = strHUI.Length;
DONG NAI UNIVERSITY OF TECHNOLOGY
String Methods
Name
public static int Compare
( string strA, string strB )
Description
Compares two specified String objects and returns an integer that
indicates their relative position in the sort order
0 strA equals strB
1 strA is greater than strB
-1 strA is less than strB
DONG NAI UNIVERSITY OF TECHNOLOGY
String Methods
Example
int nRet = string.Compare(“A", “a");
nRet=1
int nRet = string.Compare(“a", “A");
nRet=-1
int nRet = string.Compare(“a", “a");
nRet=0
DONG NAI UNIVERSITY OF TECHNOLOGY
String Methods
Name
public static int Compare(
string strA, string strB,
bool ignoreCase )
Description
Compares two specified String objects ,ignoring or honoring their
case, and returns an integer that indicates their relative position in
the sort order
DONG NAI UNIVERSITY OF TECHNOLOGY
String Methods
Example
int nRet = string.Compare
(“A", “a”,true);nRet=0
int nRet = string.Compare
(“a", “A”,false);nRet=-1
int nRet = string.Compare
(“A", “a”,false);nRet=1
DONG NAI UNIVERSITY OF TECHNOLOGY
String Methods
Name
public int CompareTo( string strB )
Description
Compares this instance with a specified String object and indicates
whether this instance precedes, follows, or appears in the same
position in the sort order as the specified String. (0
;1 ;
-1)
DONG NAI UNIVERSITY OF TECHNOLOGY
String Methods
Example
string strA = “A”;
string strB = “B”;
int nRet = strA.CompareTo(strB);
nRet=-1
string strA = “A”;
string strB = null;
int nRet = strA.CompareTo(strB);
nRet=1
DONG NAI UNIVERSITY OF TECHNOLOGY
String Methods
Name
public static string Copy(string str)
Description
Creates a new instance of String with the same value as a
specified String.
string str = string.Copy("Teo");
string str1 = "Ti";
string str2 =string.Copy(str1);
DONG NAI UNIVERSITY OF TECHNOLOGY
String Methods
Name
public bool Contains( string value )
Description
Returns a value indicating whether the specified String object
occurs within this string
Return true or false
DONG NAI UNIVERSITY OF TECHNOLOGY
String Methods
Example
string strA = "Trần Văn Tèo";
string strB = "Văn";
bool bRet = strA.Contains(strB);
if (!bRet)
MessageBox.Show("Khơng có
["+strB+"] trong ["+strA+"]");
else
MessageBox.Show("Có [" + strB + "]
trong [" + strA + "]");
DONG 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
DONG NAI UNIVERSITY OF TECHNOLOGY
String Methods
Example
string strA = "<div>Tèo </div>";
string strB = "</div>";
bool bRet = strA.EndsWith(strB);
if (bRet)
MessageBox.Show("có [" +
strB+"] ở cuối chuỗi");
else
MessageBox.Show("Ko có [" +
strB + "] ở cuối chuỗi");
DONG NAI UNIVERSITY OF TECHNOLOGY
String Methods
Name
public bool EndsWith( string value, StringComparison
comparType )
Description
Determines whether the end of this string instance matches the
specified string when compared using the specified comparison
option
DONG NAI UNIVERSITY OF TECHNOLOGY
String Methods
Example
string strA = "<div><div>Tèo</div></div>";
string strB = "</DiV>";
bool bRet = strA.EndsWith(strB,
StringComparison.OrdinalIgnoreCase);
if (bRet)
MessageBox.Show("có [" + strB+"] ở
cuối
chuỗi");
MessageBox.Show("Ko có [" + strB +
"] ở cuối chuỗi");
else
DONG 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
DONG NAI UNIVERSITY OF TECHNOLOGY
String Methods
Example
string strFormat;
strFormat = string.Format("Tỉ giá {0:d} = {1:c}", DateTime.Today,
22.3);
MessageBox.Show(strFormat);
HO CHI MINH UNIVERSITY OF INDUSTRY
Author: Duy Thanh Tran – Phone : 0987773061- Email: – Blog:
DONG NAI UNIVERSITY OF TECHNOLOGY
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
DONG NAI UNIVERSITY OF TECHNOLOGY
String Methods
Example
string str = "N guyễễ
n ThịLong Lanh C hòng C hành Ánh Tuyễế
t";
int nRet= str.IndexO f('T');
nRet= 7
int nRet= str.IndexO f(‘t');
nRet= 41
int nRet= str.IndexO f(‘Ă');
nRet= -1