1. Conversion of character arrays to strings
(1) ToCharArray () Converts a string into a character array
String s= ' I like blogs ';
Char[] Chs=s.tochararray ();
(2) The character array new string () gets the string
S=new string (CHS);
2. Determine if the string is empty: IsNullOrEmpty (), the return value is type bool
String S=null:
if (String.IsNullOrEmpty (s))
{
Console.WriteLine ("String is empty");
}
Else
{
Console.WriteLine ("String is not empty");
}
3. String comparison is equal: equal (), which compares the address (this sentence is not quite understood in some places), stringcomparison.ordinalignorecase means ignoring case
String s1= "ABCD";
String s2= "ABCD";
if (S1. Equal (S2,stringcomparison.ordinalignorecase))
{
Console.WriteLine ("Same");
}
Else
{
Console.WriteLine ("Different");
}
4. Find the position of a character in a string
(1) IndexOf (): Find the first place a character appears in a string
String s= "I like blogs";
int index=s.indexof (' Hi ');//The result is index=1
(2) LastIndexOf ()
String s= "I like blogs, like here";
int index=s.lastindexof (' Hi ');//The result is index=6
(3) Two methods can be followed by a character parameter after a shape parameter, indicating the beginning of the first search
String s= "I like blogs";
int index=s.indexof (' Hi ', 1);//start with the first one, including the first
5. String intercept function substring ()
String s= "ABCDEFG";
S=s.substring (1,4);//means intercept from the first, intercept to fourth but not fourth; the second argument does not, which means intercept to the very end
6.split ()
string s = "A,--B";
string[] NewS = S.split (new char[] {", '-', ', '}, stringsplitoptions.removeemptyentries);//new char[] {to remove The characters in the S string},stringsplitoptions.removeemptyentries the parameters of the The function is to remove the space in the resulting new string array
News[0]= "a";
News[1]= "B";
7. String insertion function join ()
String[] names = {"Zhang San", "John Doe", "Harry", "Zhao Liu"};
string S1 = string. Join ("|", names);//s1= "Zhang San | john Doe | harry | Zhao LIU |"
String s2 = string. Join ("|", 1,3.14,true, ' C ', 5000m, "Zhang San");//s2= "1|3.14|true|c|5000| Zhang San |"
8. String formatting function format ()
String s=string. Format ("I am {0} years old}",//s= "I am 20 years old this year")
Note: This article is for my study summary use, if has the infringement behavior, please contact me qq:2216297280, I will delete as soon as possible. Also welcome all the alumni to come to comment, thank you!
C # (String method Summary)