C # string processing

Source: Internet
Author: User

In C # programming, there are many measures to deal with strings, such as removing the whitespace "trim" at both ends of the string, removing the character ' 0 ' at both ends of the string, and giving the string a certain length of "padleft,padright" and so on.

Since this time the project is used more, based on this, the string of some common things to summarize.

Use STR to represent a string.

1. Length of the string :

Str. Length;

2. Get each character in the string:

Here it is necessary to speak of a function, just seen, quite excited, ToCharArray (), function said below.

A string type variable can be considered a read-only group of char variables, so you can

To get each character in a character array by ToCharArray (), here's a small example: string str= "I am a student";
char[] MyChar = str. ToCharArray ();

3. Case conversion of characters in a string:

Str. ToLower (); Str. ToUpper ().

4. Whitespace in the string, etc.

Str. Trim (); Remove all the leading and trailing spaces of the string (not including the middle). You can also delete other characters, as long as you specify them in a char array. Give me a small example.

1       string str="   "; 2       Char [] trimchars={','t','I' }; 3       str = Str. Trim (TrimChars); 4       Console.WriteLine (str);       

Output: AM A Studen

Trim () only removes all the characters that are drawn from the string.

TrimStart (), TrimEnd (), similar to trim ().

Here are two ways to add a space, str. PadLeft (); Str. PadRight (), there are two overloaded methods.

Str. PadLeft (20), specifying the length of the new string as 20,str. PadLeft (20, ' # '), in addition to the specified length, the number of digits is not enough to fill with ' # '. These two methods are used to align strings.

5. String comparison size:

String.Compare (str1, str2);

This is not the comparison length, the return value has three, STR1>STR2, the result is 1;< result for the -1;= result is 0. "Stu" > "Stu". If you only compare two strings for equality, use STR1. Equals (STR2), which returns true and false.

6. Find whether a string contains the specified string.

public bool Contains (string str1); Example: if (str. Contains ("abc")) {...}

7. Find the position of a character or substring that appears in the string.

1   string str="student"; 2 3    Str. IndexOf ("tu"
Str. IndexOf ("t"
Str. IndexOf ('t');

The return value is all 1.

Another overloaded mode: public int IndexOf (string str,int startIndex);

If these two methods are not found, the return value is-1.

The LastIndexOf () usage is the same as indexof (), except that the lookup is in the opposite direction, but still from the front.

such as: int i= str. LastIndexOf (' n '); The value of I is 5.

8. intercept the string:

Two kinds of overloaded methods

1, str. Substring (3); A parameter indicates that the character from Subscript 3 begins to intercept to the last.

2, str. Substring (1,3); two parameters indicate starting with a character subscript 2, and intercepting a substring of length 3.

9. Inserting, deleting, and replacing strings.

Insert: public string Insert (int startindex,string value); There is only one way to overload it.

Delete: public string remove (int startIndex); Removes characters from startindex to the end.

The public string, remove (int startindex,int count), removes the character starting from StartIndex with a length of count.

Replace: public string Replace (string oldstring,string newstring);

public string Replace (char OldChar, char Newchar);

10. String merging, this is not used much.

1  string [] arr={"I","am","a",  "student"}; 2  Console.WriteLine (string. Join ("@", arr));

Output:[email protected]@[email protected]

11.Split () method . This is a lot of use.

Public string[] Split (char[] separator);

1. Delimited by string:
1 usingSystem.Text.RegularExpressions;2 stringStr="AAAJSBBBJSCCC";3 string[] Sarray=regex.split (str,"JS", regexoptions.ignorecase);4 foreach(stringIinchSarray) Response.Write (i.tostring () +"<br>");

Output Result:
Aaa
Bbb
Ccc

2, separated by multiple characters:

1 stringStr="aaajbbbscccjdddseee"; 2 string[] sarray=str. Split (New Char[2] {'J','s'}); 3 foreach(stringIinchSarray) Response.Write (i.tostring () +"<br>");
Output Result:
Aaa
Bbb
Ccc
Ddd
Eee
3. Separate the characters with a single character:
1 string str="aaajbbbjccc"; 2 string [] sarray=str. Split ('J'); 3 foreach (stringin"<br>");
Output Result:
Aaa
Bbb
Ccc

C # string processing

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.