[Reading Notes] C # advanced programming Chapter 9 string and regular expressions,
(1)System. String class
System. String is a class used to store strings and allow many operations on strings. C # provides the keyword string and related syntax to make it easier to use this class.
Example:
You can use the operator overload to connect strings:
string message1 = "hello";message1 += " world";Console.WriteLine(message1);
Run the above Code and the result is as follows:
Example:
C # also allows the use of syntax similar to the index to extract characters:
string message1 = "hello";char char1 = message1[0];Console.WriteLine(char1);
Run the above Code and the result is as follows:
There are many other methods for the System. String class.
1,Create a string
There is a problem with the String class: the efficiency of modifying a given String repeatedly is very low. It is actually an immutable data type. Once the String object is initialized, this string object cannot be changed.
Example:
string message1 = "hello";message1 = "hello world";
On the surface, message1 modifies the string content. In fact, it creates a new string hello world and points it to meesage1. The original hello string has no variable to reference it, the next garbage collector is deleted.
2,StringBuilder Member
There are many reloads available when instantiating StringBuilder, which provide a string and a given capacity.
Example:
StringBuilder strb1 = new StringBuilder("hello");StringBuilder strb2 = new StringBuilder(100);
You can also set the capacity at any time. When the string length exceeds the limit, an exception is thrown:
StringBuilder strb = new StringBuilder ("1 character"); strb. Capacity = 1;
Run the above Code and the result is as follows:
The processing that can be performed on the StringBuilder class is limited to replacing or appending or deleting text in strings. The only way to convert StringBuilder to string is to use the ToString () method. The StringBuilder class should basically be used when processing multiple strings.
3,Format String
Example:
Console. writeLine ("Zhang Sanyou deposit: {1000: C}",); Console. writeLine ("Li Si deposit: {800: C}",); Console. writeLine ("Zhao Liu deposit: {20000: C );
Run the above Code and the result is as follows:
The {0, 10: C} is used to explain the function. {0} is a placeholder, the number after a comma indicates the character width to be occupied by the string, and the letter after the colon is a format specifier. When the format specifier is used, the system checks whether the type of the placeholder replacement content implements the IFormattable interface. If not, the ToString () method of the object is called.
Example:
Implement IFormattable in a custom class
1 class Program 2 {3 static void Main (string [] args) 4 {5 Person p = new Person (); 6 p. firstName = "zhang"; 7 p. secondName = "san"; 8 Console. writeLine ("unformatted specifier: {0}", p); 9 Console. writeLine ("format specifier C: {0: C}", p); 10 Console. writeLine ("format specifier A: {0: A}", p); 11} 12 13} 14 public class Person: IFormattable15 {16 public string FirstName {get; set ;} 17 public string SecondName {get; set;} 18 public string ToString (string format, IFormatProvider formatProvider) 19 {20 // call ToString () directly if no format specifier is input () 21 if (format = null) 22 {23 return ToString (); 24} 25/C returns the Chinese format name. A returns the US format name 26 switch (format. toUpper () 27 {28 case "C": 29 return FirstName + SecondName; 30 case "A": 31 return SecondName + FirstName; 32 default: 33 return ToString (); 34} 35} 36}
Run the above Code and the result is as follows:
(2)Regular Expression
The. NET Regular Expression Engine is used for regular expressions compatible with Perl 5.
1,Regular Expression Overview
A regular expression is a language specifically used for string processing.
2,Use of. NET regular expressions
Example:
MatchCollection match = Regex. Matches ("happy hapy hay", "ha. +? Y "); foreach (Match item in match) {Console. writeLine ("{0} location, matched to {1}", item. index, item. value);} Console. readKey ();
Run the above Code and the result is as follows:
For more information about regular expressions,. NET mainly involves Regex-related classes.