C # (String method Summary)

Source: Internet
Author: User

1. Conversion of character arrays to strings

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

String s= ' I like blogs ';

Char[] Chs=s.tochararray ();

(2) The character array new string () gets the string

S=new string (CHS);

2. Determine if the string is empty: IsNullOrEmpty (), the return value is type bool

String S=null:

if (String.IsNullOrEmpty (s))

{

Console.WriteLine ("String is empty");

}

Else

{

Console.WriteLine ("String is not empty");

}

3. String comparison is equal: equal (), which compares the address (this sentence is not quite understood in some places), stringcomparison.ordinalignorecase means ignoring case

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 a string

(1) IndexOf (): Find the first place a character appears in a string

String s= "I like blogs";

int index=s.indexof (' Hi ');//The result is index=1

(2) LastIndexOf ()

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

int index=s.lastindexof (' Hi ');//The result is index=6

(3) Two methods can be followed by a character parameter after a shape parameter, indicating the beginning of the first search

String s= "I like blogs";

int index=s.indexof (' Hi ', 1);//start with the first one, including the first

5. String intercept function substring ()

String s= "ABCDEFG";

S=s.substring (1,4);//means intercept from the first, intercept to fourth but not fourth; the second argument does not, which means intercept to the very end

6.split ()

string s = "A,--B";

string[] NewS = S.split (new char[] {", '-', ', '}, stringsplitoptions.removeemptyentries);//new char[] {to remove                                                                                                                                              The characters in the S string},stringsplitoptions.removeemptyentries the parameters of the The function is to remove the space in the resulting new string array

News[0]= "a";

News[1]= "B";

7. String insertion function join ()

String[] names = {"Zhang San", "John Doe", "Harry", "Zhao Liu"};

string S1 = string. Join ("|", names);//s1= "Zhang San | john Doe | harry | Zhao LIU |"

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

8. String formatting function format ()

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

Note: This article is for my study summary use, if has the infringement behavior, please contact me qq:2216297280, I will delete as soon as possible. Also welcome all the alumni to come to comment, thank you!

C # (String method Summary)

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.