C # programming (9_string)

Source: Internet
Author: User

A string type variable can be considered a read-only group of variables of type char, so that each string can be accessed using the following syntax:

string mystring="A string"; Char mychar=mystring[1];

However, you cannot assign values to individual strings in this way. In order to obtain a writable char array, you can use the following code, which uses the ToCharArray () command of the array variable:

string mystring="A string"; Char [] Mychars = Mystring.tochararray ();

You can then work with the char array in a standard way. You can also use strings in a foreach loop. For example:

foreach ( in myString) {   Console.WriteLine ("[0]", character);}    

As with arrays, you can also use Mystring.length to get the number of elements, which gives the number of characters in the string, for example:

string myString = console.readline (); Console.WriteLine ("", mystring.length);

The basic processing techniques for other strings are used with this <string>. The ToCharArray () command uses the command in a similar format. Two simple but very effective command <string> ToLower () and <string>. ToUpper (). They can convert strings to uppercase and lowercase, respectively. Consider the following scenario: To check a response from a user, such as a string yes. If you can convert a user-entered string to lowercase, you can also check the string yes, yes, yes, and so on.

namespaceconsoleapplication1{classProgram {Static voidMain (string[] args) {            stringmystring="This is a test."; //separating each string in a space will get its substring, that is, an array containing a single word.            Char[] seperator={' '}; string[] mywords; Mywords=Mystring.split (seperator); //Use the Foreach loop to iterate through the words in the array and write the words to the console            foreach(stringWordinchmywords) {Console.WriteLine ("{0}", Word);        } console.readkey (); }    }}// This// is//a//test.

Use <string>. Split () Converts a string into a string array, separating it from the specified position. These locations are in the form of a char array, with only one element, the space character, in the example above.

C # programming (9_string)

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.