[C #] There is no unspeakable secret between string, big S, and small S,

Source: Internet
Author: User
Tags string methods

[C #] There is no unspeakable secret between string, big S, and small S,
There is no unspeakable secret directory between string, String, big S, and small S.

  • Lowercase string and uppercase String
  • Declare and initialize a string
  • Immutable string
  • Regular string and original string
  • String escape sequence
  • Format a string
  • Operation substring
  • Null and "" (null) of the string)
  • High-performance StringBuilder

 

Collation

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}

Figure: var s1 = "Hi! "; Var s2 =" Fanguzai! ";

Figure: s1 = s1 + s2; Re-Modify the s1 point

 

Because the "modify" string is actually creating a new string, you must be cautious when creating a reference to the string. If you create a reference to the string and then "modify" the original string, the reference still points to the original object, rather than the new object created when the string is modified.

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}

Figure: var s1 = "Hi! "; S2 = s1; they point to the same reference address

Figure: s1 = s1 + "Fanguzai! "; A" Fanguzai "that is not referenced will be created! ", And re-Modify the value pointed to by s1.

 

Regular string and original string

If the Escape Character provided by C # Must be embedded, use the regular string:

 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

1 private static void Main (string [] args) 2 {3 const string name = "Fanguzai"; 4 var s = string. Format ("Hi, {0 }! ", Name); // use the placeholder 5 6 Console. WriteLine (s); 7 8 Console. Read (); 9}

1 private static void Main (string [] args) 2 {3 const string s1 = "Hi, Fanguzai! "; 4 5 Console. writeLine (s1.Substring (4); // captures 6 consoles. writeLine (s1.Replace ("Hi", "Hello"); // replace 7 Console. writeLine (s1.IndexOf (",", StringComparison. ordinal); // index 8 9 Console. read (); 10}

Var s = string. Empty;

On the contrary, the null String does not reference the instance of the System. String object. Any attempt to call the method of the null String will generate NullReferenceException. However, the null string can be used with other strings in the concatenation and comparison operations.

1 private static void Main (string [] args) 2 {3 const string s1 = "Hi, Fanguzai! "; 4 string s2 = null; 5 var s3 = string. empty; 6 7 var s4 = s1 + s2; // concatenate a string with a value with null. writeLine ("s4: {0}", s4); 9 Console. writeLine (""); 10 11 var isTrue = (s2 = s3); 12 Console. writeLine ("isTrue: {0}", isTrue); 13 Console. writeLine (); 14 15 var s5 = s3 + s2; 16 Console. writeLine ("s5: {0}", s5); 17 Console. writeLine (); 18 19 Console. read (); 20}

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 hundreds of millions 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 can be reassigned to 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         }

C # knowledge Review-delegated delegate and C # knowledge Review-delegated delegate (continued)

C # knowledge Review-Introduction to events

 

 

[Blogger] Anti-Bot

[Source] http://www.cnblogs.com/liqingwen/p/6155790.html

[Reference] Microsoft official documentation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.