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.
Stringbuilder members:
Stringbuilder sb = new stringbuilder ("www.sinory.com"); // defines the object whose initial value is www.sinory.com.
Stringbuilder sb = new stringbuilder (20); Initialize an empty object with a capacity of 20.
In addition, stringbuilder also has the maxcapacity attribute to limit the maximum capacity that an object can use. The default value is approximately Int. maxvalue (2 billion)
SB. maxcapacity = value can be defined during use;
SB. append () to append a string to the current string.
SB. appendformat () -- add a string of a specific format
SB. insert () -- insert a substring
SB. Remove () -- delete characters from the current string
SB. Replace () -- replace the string with the specified region address? BR> Sb. tostring () -- converts sb to a string object