[C #] secrets about strings between string, large S, and small S (sorting ...),
Secrets about strings between string, large S, and small S
A String is a String object whose value is text. Internally, the text is stored as a sequential read-only set of Char objects. The C # string does not end with null. Therefore, the C # string can contain any number of embedded null characters ("\ 0 "). The Length attribute of a string representsCharThe number of objects, not the number of Unicode characters. To access each Unicode code bit in a string, use the StringInfo object.
Lowercase string and uppercase String
In C #, the keyword string is the alias of String. Therefore, String is equivalent to string, that is, you can use whatever you want. The String class provides many methods for securely creating, operating, and comparing strings. In addition, the C # language also loads some operators to simplify common string operations.
Declare and initialize a string
See the example:
1 static void Main (string [] args) 2 {3 // declare but do not initialize 4 string msg1; 5 6 // declare and initialize to null 7 string msg2 = null; 8 9 // initialize as an Empty string. Use the Empty (null) constant instead of the "" (null) 10 String msg3 = string. empty; 11 12 // use the regular string literal value to initialize 13 string oldPath = "c: \ windows "; 14 15 // initialize 16 string newPath = @ "c: \ windows"; 17 18 // you can also use System. string19 String content = "Hello World! "; 20 21 // use const to prevent msg4 from being tampered with 22 const string msg4 =" I'm const! "; 23 24 // implicit var25 var msg5 =" Hi! "; 26 27 // use the String constructor to initialize 28 char [] letters = {'A', 'B', 'C '}; 29 string alphabet = new String (letters); 30 31 Console. read (); 32}
Note: Do not use the new operator to create a String object except when initializing a string using a character array.
You can use the Empty constant value to initialize a String to create a String object with zero length. The string representation of a zero-length string is "". Using the Empty value instead of null to initialize a string can reduce the possibility of NullReferenceException. We often use the static IsNullOrEmpty (String) method to verify the String value before trying to access the String.
Immutable string
String objects are unchangeable: they cannot be changed after being created. All the seemingly modified String methods and operators in C # actually return results in the form of new String objects. In the following examples1Ands2To form a string, the two original strings are not modified.+=The operator creates a new string that contains the combined content. This new object is assigned to the variable.s1And initially assigneds1The object is released because no other variables include references to it, and will be reclaimed in the future.
Example 1:
1 static void Main (string [] args) 2 {3 var s1 = "Hi! "; 4 var s2 =" Fanguzai! "; 5 6 // splice s1 and s2, and modify the value pointed to by s1 7 s1 + = s2; // that is, s1 = s1 + s2; 8 9 Console. writeLine (s1); 10 Console. read (); 11}
1 static void Main (string [] args) 2 {3 var s1 = "Hi! "; 4 var s2 = s1; 5 6 // After the s1 value is assigned again, this time the value pointed to by s2 is not modified. 7 s1 + =" Fanguzai! "; // S1 = s1 +" Fanguzai! "; 8 9 Console. WriteLine (s2); 10 Console. Read (); 11}
1 static void Main (string [] args) 2 {3 var coluString = "Col1 \ tCol2 \ tCol3"; 4 var rowString = "Row1 \ r \ nRow2 \ r \ nRow3 "; 5 6 Console. writeLine (coluString); 7 Console. writeLine ("===="); 8 Console. writeLine (rowString); 9 Console. read (); 10}
1 static void Main (string [] args) 2 {3 var path = @ "C: \ Windows"; 4 var text = @ "Are you Fanguzai? 5 I'm Fanguzai! "; 6 7 Console. writeLine (path); 8 Console. writeLine ("===="); 9 Console. writeLine (text); 10 Console. read (); 11}
String escape sequence
High-performance StringBuilder
The string operations in. NET are highly optimized and will not significantly affect performance in most cases. However, in some application scenarios, for example, in a loop that executes hundreds or even thousands of times, string operations may affect performance. The StringBuilder class creates a string buffer to provide better performance when a program executes a large number of string operations. The StringBuilder string also enables you to reassign individual characters (characters not supported by the built-in string data type ). For example, this code changes the content of a string without creating a new string:
1 static void Main(string[] args)2 {3 var sb = new StringBuilder("~ Hi! Fanguzai!");4 sb[0] = '^';5 6 Console.WriteLine(sb);7 Console.Read();8 }
[Blogger] Anti-Bot
[Source] http://www.cnblogs.com/liqingwen/p/6155790.html
[Reference] Microsoft official documentation