String (1), string (
String
Famous string variable string myString = "first string ";
Display string Console. WriteLine ("myString =" + myString );
When you want to perform large-scale character replacement and adjustment operations, using the String class will seriously affect the performance. In this case, you can use the System. Text. StringBuilder class. The StringBuilder class does not have the powerful functions of the String class. It only provides the basic function of replacing, adding, and deleting text in strings, but it is very efficient.
Sb. Length indicates the actual Length of the string.
Sb. Capacity indicates the memory space occupied by the string
Sb. Append () Append the string to the current string
Sb. AppendFormat () add strings in a specific format
Sb. Insert () Insert a substring
Sb. Remove () Remove characters from the current string
Sb. Replace () replaces the string specified in the string
Sb. ToString () converts sb to a String object
Connection string
Use the Concat () method
For example, string myString1 = string. Concat ("hello,", "friend ");
Console. WriteLine ("String. Concat (\" Friend, \ ", \" Hello \ ") =" + myString1 );
// String. Concat ("friend,", "hello") = Hello, friend
Use + operator
For example, string myString1 = "hello," + "friend ";
Console. WriteLine ("\" Friend, \ "+ \" Hello \ "=" + myString1 );
// "Hello," + "friend" = Hello, friend
Use the jion () method
String [] myStrings = {"you", "good "};
String myString = String. jion ("-", myStrings );
ConSole. WriteLine ("myString =" + myString );
// You-OK
Evaluate string substrings
When we need to obtain a SubString from a String, we can use the SubString () method of the String class, which returns a SubString of the specified length starting from the specified position.
The SubString () method of the String class has two reloads:
SubString (int startIndex); returns the SubString from startIndex to the end of the string.
SubString (int startIndex, int lenght); returns a child string with a length of lenght starting from startIndex.
String myString = "www.fecit.com.cn ";
String myString1 = myString. SubString (3 );
Console. WriteLine ("String. SubString (3) =" + myString1 );
// String. SubString (3) = .fecit.com.cn
String mystring2 = myString. SubString (3,2 );
Console. WriteLine ("String. SubString (3,2) =" + myString2); // String. SubString (3,2) =. f
Delete character
To delete a specific character from a string, you can use the Remove () method.
The Remove () method of the String class has two reloads:
Public string Remove (int startIndex); returns the string from startIndex to the end of the string.
Public string Remove (int startIndex, int count );
The parameter startIndex is used to specify the location where the deletion starts, starting from 0 and count to specify the number of characters to be deleted.
A space is also a character.
String MyString = "Hello UnderWorld ";
Console. WriteLine (MyString. Remove (7,5 ));
// Hello World