C # (string method summary ),

Source: Internet
Author: User

C # (string method summary ),

1. character array and String Conversion

(1) ToCharArray () converts a string into a character array

String s = 'blog I like ';

Char [] chs = s. ToCharArray ();

(2) obtain the string from the character array new string ().

S = new string (chs );

2. Determine whether the string is null: IsNullOrEmpty (). The return value is of the bool type.

String s = null:

If (string. isNullOrEmpty (s ))

{

Console. WriteLine ("the string is empty ");

}

Else

{

Console. WriteLine ("the string is not empty ");

}

3. Check whether the string comparison is equal: equal (), where the address is compared (which is not quite understandable in some cases), and StringComparison. OrdinalIgnoreCase indicates case-insensitive.

String s1 = "abcd ";

String s2 = "ABCD ";

If (s1.Equal (s2, StringComparison. OrdinalIgnoreCase ))

{

Console. WriteLine ("same ");

}

Else

{

Console. WriteLine ("different ");

}

4. Find the position of a character in the string

(1) IndexOf (): searches for the first position of a character in the string.

String s = "I like blogs ";

Int index = s. IndexOf ('Xi '); // The result is index = 1.

(2) LastIndexOf ()

String s = "I like blogs, like this ";

Int index = s. LastIndexOf ('H'); // The result is index = 6.

(3) Both methods can be followed by an integer parameter after the character parameter, indicating to start searching from the first few

String s = "I like blogs ";

Int index = s. IndexOf ('h', 1); // search for the first index, including the first index.

5. String truncation function Substring ()

String s = "abcdefg ";

S = s. Substring (); // It indicates to intercept from the first one, and to the fourth one, but not the fourth one. The second parameter is optional, indicating to intercept to the end.

6. split ()

String s = "a, -- B ";

String [] newS = s. split (new char [] {'', '-', ','}, StringSplitOptions. removeEmptyEntries); // new char [] {characters in the s string to be removed}, StringSplitOptions. the RemoveEmptyEntries parameter is used to remove spaces from the new string array.

NewS [0] = "";

NewS [1] = "B ";

7. String insertion function Join ()

String [] names = {"Zhang San", "Li Si", "Wang Wu", "Zhao Liu "};

String s1 = string. Join ("|", names); // s1 = "Zhang San | Li Si | Wang Wu | Zhao Liu |"

String s2 = string. join ("|", 1, 3.14, true, 'C', 5000 m, "James "); // s2 = "1 | 3.14 | true | c | 5000 | James |"

8. String Formatting Function Format ()

String s = string. Format ("I am {0} years old}", 20); // s = "I am 20 years old"

 

Note: This article is used for my summary. If any infringement happens, contact qq: 2216297280. I will delete it as soon as possible. You are also welcome to comments. Thank you!

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.