. NET String Common operations

Source: Internet
Author: User

1. Take the length of the string

String str= "China";

int len=str.length;

2, string converted to bit code GetBytes

Byte[] Bytstr=system. Text.Encoding.Default.GetBytes (str);

3. String Miss Home StringBuilder ()

System.Text.StringBuilder sb=new System. Text. StringBuilder ();

Sb. Append ("Zhonghua");

Sb. Append ("The People");

Sb. Append ("Republic");

and "+" equivalent output of the People's Republic of China

4. String intercept Substring (starting position, intercept digit);

String str= "ASDFG";

String s1=str.substring (0,2);

S1=as;

5. Check whether the specified position is a null character char. ISWHITESPCE (string variable, number of digits);

String Str= "People's Republic of China";

Response.Write (Char. Iswhitespace (str,2));

Result is True

6. Whether the Chag is a punctuation char. Ispunctuation (' character ');

Method Ibid.

7. Convert characters to numbers

Response.Write ((int) ' Medium ');

8. Convert numbers to Characters

Response.Write ((char) 22269);

The result is the state word

9 Clear the space before and after the string Trim ()

String str= "China";

Str=str.trim ();

10. Replace the string string variable. Replace ("original character", "replacement character");

Example:

String str = "China";

Str=str. Replace ("Country", "central");

Str= "Central";

11. Delete the last character of the string lost method

String: String s= "123456";

Method One

S=s.substring (0,s.length-1);

Method Two

S=s.trimend (', ');

Char[] mychar={' 5 ', ', '};

S=s.trimend (MyChar);

12. String Split Split

Method One: Split with a single character

String str= "AAA,BBB,CCC";

String[] Sarray=str. Split ('. ');

foreach (String i in Sarray)

{

Response.Write (i.ToString ());

}

Results:

Aaa

Bbb

Ccc

Method Two: Divide with multiple characters

String str = "AAAJBBBSCCC";

string [] sarray=str. Split (new char[] {' J ', ' s '});

foreach (String i in Sarray)

{

Response.Write (i.ToString ());

}

Results:

Aaa

Bbb

Ccc

Method Three: Use String to divide

Using System.Text.RegularExpressions; Reference first

String str = "AAAJSBBBJSCCC";

string []sarray=regex.split (str, "JS", regexoptions.ignorecase);

foreach (String i in Sarray)

{

Response.Write (i.ToString ());

}

Results:

Aaa

Bbb

Ccc

13. The method of converting 123456789 to 12-345-6789

Method One

String a= "123456789";

A=int. Parse (a). ToString ("##-###-####");

Method Two

A=a.insert (5, "-"). Insert (2, "-");

Method Three

Using System.Text.RegularExpressions; Reference first

Regex reg =new Regex (@ "^ (d{2}) (D{3}) (D{4}) $");

A=reg. Replace (A, "$1-$2-$3");

14. Output 21 A simple notation

String Str=new string (' A ', 21);

15. Methods for obtaining random numbers

Random r=new random ();

int N1=r.next ();

Respons.write (N1);

16. Converting a string into an integer number

String mystring= "1234";

int myint=0;

Int32.TryParse (Mystring,out Myint);

Response.Write (Myint);

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.