C # string basics of Programming

Source: Internet
Author: User

1. in C #, a single string is of the char type ('A') enclosed in single quotes and can only contain one character.

2. A single character can also be expressed as a string, or a string of 0 length.

3. Use the S. Length attribute to obtain the number of characters in the string.

4. string can be viewed as a char-Type Read-Only array. Char c = s [1]; example: traverse each element in the output string.

Character strings in C # have an important feature: immutable. Once declared, the string can no longer be changed.

Therefore, only the char at the specified position can be read through the index, and the char at the specified position cannot be modified.

6. if you want to modify char, you must create a new string using S. the tochararray () method obtains the char array of the string. After modifying the array, call the new string (char []) constructor. To create a char array string. Once a string is created, the modification of the char array will not change the string.

7. String S = "ABC"; S = "123"; does s change?

Resolution: differentiate between the variable name and the value pointed to by the variable. There are many strings in the program, which can be pointed to by string variables. variables can point to other strings. However, the string itself does not change. The immutable character string is worth the immutable character string in the memory.
Is variable unchanged.

Example 1:

Using system; using system. collections. generic; using system. text; namespace string basics {class program {static void main (string [] ARGs) {string S1 = "hello"; // defines a string char [] chars = s1.tochararray (); // store the string in the character array chars [0] = 'a'; // modify the first element of the character array to 'A' string S2 = new string (chars ); // create a new string to save the Chars content of the character array. Console. writeline ("S1 = {0}", S1); // changing chars does not change chars because chars is a copy of the console. writeline ("S2 = {0}", S2); // print the string S2. S2 prints alleo because of the new value, but the string will not change in the future. Console. readkey ();}}}

Running result:

Example 2:

 

The source code is as follows:

Using system; using system. collections. generic; using system. text; namespace string basics {class program {static void main (string [] ARGs) {string S = "hello"; // defines a string console. writeline (s); // s points to the string "hello" s = "yello"; console. writeline (s); // s points to the string "yello", s points to change, but the string itself does not change. Console. readkey ();}}}

Program:

Example 3:

The source code is as follows:

Using system; using system. collections. generic; using system. text; namespace string basics {class program {static void main (string [] ARGs) {string S1 = "hello"; // defines a string S2 = S1; // S2 points to the string that S1 points to, instead of S2 points to S1. Even if S1 points to other memory later, S2 still points to "hello" char [] chars = s1.tochararray (); // store the string in the character array chars [0] = 'a'; // modify the first element of the character array to 'A' S1 = new string (chars ); // create a new string to save the Chars content of the character array. Console. writeline ("S1 = {0}", S1); // changing chars does not change chars because chars is a copy of the console. writeline ("S2 = {0}", S2); // crossing the river to split the bridge, and the change of S1 does not have any relationship with the console. readkey ();}}}

Program running:

 

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.