1. Comparing Strings
The String class provides a series of methods for string comparisons, such as the CompareTo and equals methods.
①compareto:
Returns 0 if the value of the argument is equal to this instance, or 1 if this instance is greater than the value of the parameter; otherwise returns-1.
String str1 = "abc";
int m1 = Str1.compareto ("abc"); M1 = 0
int m2 = str1.compareto ("ab"); M2 = 1
int m3 = Str1.compareto ("ABCD"); M3 =-1
②equals
True if the value of the parameter is the same as this instance; otherwise false.
String str1 = "abc", STR2 = "abc", STR3 = "abc";
BOOL B1 = str1. Equals (STR2); False
BOOL B2 = str1. Equals (STR3);//true
Note: The Equals method is case-sensitive.
2. Locating characters and substrings
Position the first occurrence of a character or substring in a string using the IndexOf method.
If the character is found, the index position is the value of the parameter, starting at 0, or 1 if the character is not found, or 0 if the argument is empty.
String str1 = "ABCD";
int m1 = str1. IndexOf ("B"); 1
int m2 = str1. IndexOf ("CD");//2
int m3 = str1. IndexOf (""); 0
int m4 = str1. IndexOf ("w");//-1
3. Formatting strings
Format to currency type
String str1 = String.Format ("(C) currency:{0:c}\n", -123.45678f); (C) currency:¥-123.46
Format to ShortDate type
String str2 = String.Format ("(d) Short date:{0:d]\n", DateTime.Now); (d) Short DATE:2009/11/6
4. Intercepting strings
String str = "Hello world!";
String str1 = str. Substring (0,5);//Hello
The first parameter represents the starting position of the substring, and the second parameter represents the length of the substring.
5. Split string
The Split method splits a string into a series of small strings according to a delimiter.
String str = "hello.world!";
string[] spilt = str. Split ('. ', '! ');
foreach (string s in spilt)
{
if (S.trim ()! = "")
Console.WriteLine (s);
}
6. Inserting and populating strings
① inserting a string
String str = "This is a girl";
str = str. Insert (Ten, "Beautiful");//This is a beautiful girl
The first parameter specifies the position to insert, the index starts at 0, and the second parameter specifies the string to insert.
② padding string
String str = "Hello world!";
String str1 = str. PadLeft (15, ' @ '); @@ ZZFCTHOTFIXZ world!
String str2 = str. PadRight (15, ' @ '); Hello [Email protected]@@
The first parameter specifies the length of the string after the fill, and the second parameter specifies the character to be populated. The second argument can be omitted, and if omitted, the space symbol is filled.
7. Delete and cut strings
① Deleting a string
String str = "This is a beautiful girl.";
str = str. Remove (10,10); This is a girl.
The first parameter specifies where to start the deletion, the index starts at 0, and the second parameter specifies the number of characters to delete.
② clipping string
String str = "*_*hello world! *_*";
String str1 = str. TrimStart (new char[] {' * ', ' _ '});//hello world! *_*
String str2 = str. TrimEnd (new char[] {' * ', ' _ '});//*_*hello world!
8. Copying strings
String str= "It is a dog";
String newstr=string.copy (str);
9. Replace string
String str= "It is a dog";
Str=str. Replace ("Dog", "pig");
10. Determine if the string is empty
string str = "AA";
BOOL B = String.IsNullOrEmpty (str);
11. Determine if the string contains Chinese
String str = "AAAAA";
BOOL B = Regex.IsMatch (str, @ "[\u4e00-\u9fa5]");
12. Determine if a string contains another string
String str1 = "Hello!" XXXX ";
String str2 = "Hello";
BOOL B = str1. Contains (STR2);
C # string processing