String and string, the secret of the strings between big s and small s
The string is an object of type string and its value is text. Internally, the text is stored as a sequential read-only collection of Char objects. C # strings have no null-terminated characters at the end, so C # strings can contain any number of embedded null characters ("s"). The Length property of a string represents the number of objects it contains Char , not the number of Unicode characters. To access individual Unicode code bits in a string, use the StringInfo object.
lowercase string and uppercase string
In C #, the keyword string is the alias of a string. As a result, string is equivalent to string, which means you use whichever you want. The string class provides a number of methods for securely creating, manipulating, and comparing strings. In addition, the C # language also overloads some operators to simplify common string manipulation.
Declaring and initializing a string
Take a look at the example:
1 Static voidMain (string[] args)2 {3 //declaring but not initializing4 stringMSG1;5 6 //declaring and initializing null7 stringMSG2 =NULL;8 9 //initialized as an empty string with empty (empty) constant instead of literal "" (empty)Ten stringMSG3 =String.Empty; One A //initialize with regular string literals - stringOldPath ="C:\\Windows"; - the //initialize directly with a string - stringNewPath =@"C:\Windows"; - - //You can also use System.String +String content ="Hello world!"; - + //use const to prevent MSG4 from being tampered with A Const stringMSG4 ="I ' m const!"; at - //You can use implicit type var - varMSG5 ="hi!"; - - //initializing using the String constructor - Char[] Letters = {'A','B','C' }; in stringAlphabet =NewString (Letters); - to Console.read (); +}
Note : Do not use the new operator to create a string object except when initializing a string with a character array.
Initializes a string with the Empty constant value to create a new string object with a zero length. The string representation of a 0-length string is "". Initializing a string with an Empty value (instead of NULL) can reduce the likelihood of nullreferenceexception occurring. We often use the static IsNullOrEmpty (string) method to verify the value of a string before attempting to access the string.
Non-variability of string
String objects are immutable: that is, they cannot be changed after they are created. All string methods that appear to modify the string and operators in C # actually return the result as a new string object. In the following example, s1 s2 Two original strings are not modified when the contents of the connection and the content are formed into a string. +=operator creates a new string that contains the combined content. The new object is assigned to the variable s1 , and the object originally assigned to s1 it is freed because no other variable contains a reference to it, and is subsequently garbage collected.
Example one:
1 Static voidMain (string[] args)2 {3 varS1 ="hi!";4 varS2 ="fanguzai!";5 6 //stitching S1 and S2, and modifying the value S1 points to7S1 + = s2;//i.e. S1 = s1 + s2;8 9 Console.WriteLine (S1);Ten Console.read (); One}
Because the modify string is actually creating a new string, you must be careful when you create a reference to the string. If you create a reference to a string and then "modify" the original string, the reference points to the original object instead of the new object that was created when the string was modified.
1 Static voidMain (string[] args)2 {3 varS1 ="hi!";4 varS2 =S1;5 6 //after the S1 is re-assigned, the value pointed to by S2 is not re-modified this time7S1 + ="fanguzai!";//i.e. S1 = S1 + "fanguzai!";8 9 Console.WriteLine (S2);Ten Console.read (); One}
Regular string and literal string
Escape sequence of String
formatting strings
Manipulating substrings
Null for string with "" (empty)
StringBuilder that can be used to raise high performance
String operations in. NET are highly optimized and in most cases do not significantly affect performance. But in some scenarios, such as in a loop that performs hundreds of or even thousands of times, string manipulation is likely to affect performance. The StringBuilder class creates a string buffer that provides better performance when the 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 contents of a string without creating a new string:
1 Static voidMain (string[] args)2 {3 varSB =NewStringBuilder ("~ hi! fanguzai!");4sb[0] ='^';5 6 Console.WriteLine (SB);7 Console.read ();8}
"Bo Master" anti-bone Aberdeen
"Provenance" http://www.cnblogs.com/liqingwen/p/6155790.html
"Reference" Official Microsoft documentation
[C #] string and string, big s and small s between the secrets of the string (collation ... )