First, we will introduce several common functions:
Compare (str1, str2) -- Compare the size of two strings str1 and str2. If it is greater than the return positive number, it is equal to the return 0. If it is less than the return negative number!
IndexOf -- locate the position where a given string appears for the first time in a string
PadLeft and PadRight -- fill the string with the specified character at the beginning and end of the string
ToLower and ToUpper convert strings to lowercase or uppercase
Trim -- delete blank spaces at the beginning and end
String. Replace -- Replace the specified character in the String with the specified character.
C # string creation process:
For example, define the variable strT = "Welcome ";
StrT + = "www.sinory.com ";
The program first creates a System. String type object and initializes it as "Welcome ". At this time, the compilation level will allocate enough memory to save the text string. Use the strT variable to represent this instance. When strT + = "www.sinory.com" is executed, the system creates a new instance and allocates enough memory to save the composite text. Then, the variable strT is used to represent the new character.
String. When you want to perform large-scale character replacement and adjustment operations, using strings will seriously affect the performance. In this case, the System. Text. StringBuilder class can be used.
The StringBuilder class does not have the powerful functions of the String class. It only provides basic replacement and Addition and deletion of text in strings, but it is very efficient, when defining a StringBuilder object, you can specify the memory capacity. If no value is specified, the system determines the size based on the string length during object initialization. The parameter Length and Capacity indicate the actual Length of the string and the memory occupied by the string respectively. Modifying strings is performed in this memory, which greatly improves the efficiency of adding and replacing strings.
For example, define StringBuilder sb = new StringBuilder ("Hello, Welcome", 100); // initialize the object and set the initial capacity to 100
Sb. Append ("to www.sinory.com ");
Sb. Replace (old, new); // Replace old with new, which is the same as String. Replace () but does not need to be copied during the process.