asp.net C # common string manipulation functions

Source: Internet
Author: User
Tags foreach split

In C # There is a type that directly represents a string, a string type, and he is a collection of char characters, like a system array of char types

The. NET framework supports powerful processing of strings, such as finding, replacing, formatting, adjusting, etc.

String can be spliced with + to connect 2 strings

Traversal string

Can traverse a string like an array

The code is as follows Copy Code
String thestring = "Hello,world";
Char Thefristchar = thestringp[0];

Characters are read-only and cannot be modified for individual characters

The code is as follows Copy Code
String thestring = "Hello,world";
Thestring[0] = ' s ';//wrong

Like an array, you can get the length of a string with length

You can iterate over a string with foreach

The code is as follows Copy Code

String thestring = "Hi,world";
foreach (char s in thestring)
{
Console.Write (s);
}

A character array representing a string can be obtained through the ToCharArray () Form

The code is as follows Copy Code

string thestring = "Hello World";
char[] Thechars = Thestring.tochararray ();

Trim goes to the 2-head space, TrimStart to the specified string at the beginning, trimend the specified string to the end

The code is as follows Copy Code

string s = "aaasssddd";
string S1 = "ASASASDDD";
Console.WriteLine (S.trimstart (new char[] {' A ', ' s '})); Ddd.
Console.WriteLine (S1. TrimStart (new char[] {' A ', ' s '}); Sasasddd.

PadLeft or padright Add the specified characters at the beginning and end

The code is as follows Copy Code

string s = "12345";
Console.WriteLine (S.padleft, ' V ')); vvvvv12345
string s = "12345";
Console.WriteLine (S.padleft (5, ' V ')); 12345
string s = "12345";
Console.WriteLine (S.padleft (3, ' V ')); 12345

Split splits the string into a string fragment by the specified character, resulting in an array of strings

The code is as follows Copy Code

String str = "one Two_three=four";
string[] arr = str. Split (new char[] {', ' _ ', ' = '});
foreach (string s in arr) {
Console.WriteLine (s);
}

Substring can get the specified position to the end of the string fragment the first argument is the start position 2nd is the length of the Intercept string

The code is as follows Copy Code

String str= "0123456789";
string newstr = str. Substring (3);
Console.WriteLine (NEWSTR);
string newStr2 = str. Substring (3,1);
Console.WriteLine (NEWSTR2);

Repalce the string specified by the replacement string

The code is as follows Copy Code

String str = "ASDASDZXC";
Console.WriteLine (str. Replace (' A ', ' 1 '));

Remove deletes the string fragment at the specified position in the string the first argument is the position the 2nd parameter is the length

The code is as follows Copy Code
String str = "0123456789";
Console.WriteLine (str. Remove (5));
Console.WriteLine (str. Remove (5,1));

IndexOf find the specified string position in the string, the first argument is the string to find the 2nd parameter start position

The code is as follows Copy Code

String str = "OK is OK";
Console.WriteLine (str. IndexOf ("OK"));
Console.WriteLine (str. IndexOf ("OK", 1));

Related Article

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.