C # Getting Started-reading notes

Source: Internet
Author: User

    • Processing of strings

Common uses of string

In order to obtain a writable char array, you can use the ToCharArray () command of the array variable

String myString = "A String"; char[] Mychars = Mystring.tochararray ();

You can also use strings in a foreach loop

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

<string>. ToLower () and <string>. ToUpper () to convert the string to association or uppercase, respectively. This command creates a new string to compare with another string, or to assign another variable.

Delete the space in the input character, <string>. Trim ()

You can also use these commands to remove other characters, as long as you specify the characters in a char array

1 Char [] TrimChars = {'e's'}; 2 string trimstring = Mystring.trim (trimChars);

<string>. TrimStart () <sting>. The TrimEnd () command allows them to delete a space before or after a string. You can also specify a char array when using these commands.

<string>. PadLeft () <string>. PadRight (), add a space to the left or right of the string to make the string reach the specified length.

1 " Aligned " ; 2 myString = Mystring.padleft (ten); 3 myString = Mystring.padleft ('-');

<string>. Split () converts the sstring to a string array, separating him from the specified position, in the form of a char array

1 char[] separator = {' }; 2 string mywords; 3 mywords = mystring.split (separator);

    • Static constructors and static classes

When you use static members in a class, you need to initialize those members beforehand. At the time of declaration, you can provide a static member with an initial value, but sometimes you need to perform more complex initialization or perform some action before copying or executing a static method.

You can perform this type of initialization task using a static constructor. A class can have only one static constructor, which cannot have an access modifier or take any parameters. Static constructors cannot be called directly and can only be performed in the following cases:

1. When you create a class instance that contains a static constructor

2. When accessing static members of a class that contains static constructors

In both cases, the static constructor is called first, and then the class is instantiated or the static member is accessed. No matter how many instances of a class are created, their static constructors are called only once.

Static class functions contain static members and cannot contain instance constructors.

C # Getting Started-reading notes

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.