C # string handling

1. Remove the head and tail of the specified string

(1) Delete single character

eg : string A = "C#abcdefg#C";

① delete character before and after the emergence of A 'C',

        A = A.Trim('C');

Output: # abcdefg #

A ② delete previous character 'C',

       A = A.TrimStart('C');

Output: # abcdefg # C

A delete characters behind ③ 'C'

       A = A.TrimEnd ( 'C');

Output: C # abcdefg #

Summary: A.Trim ( 'C') is equivalent to A.TrimStart ( 'C') TrimEnd ( 'C').

(2) delete multiple characters (only to Trim function as an example)

eg:string B = "abbacdefgba"

First, look at the code B = B.Trim ( "ab" .ToCharArray ()), and delete a single character is very different

Meaning of this code is a string of characters to delete end-occurring 'a' or 'b', until neither an 'a' is not 'b' character appears, do not understood to delete the string "ab"

Output: cdefg

2. specify a character string cut

eg string A = "1111 2222 3333 4444 5555 6666";

Now we want A, met once every space cut off once and stored in the array:

string[] Arr = Regex.Split(A, " ", RegexOptions.IgnoreCase);

foreach(string item in A)

{

 Console.WriteLine(item + "\r\n");

}

Console.ReadKey();

(Need to load the namespace System.Text.RegularExpressions)

Output: 1111

     2222

                  3333

                  4444

                  5555

                  6666

3. Get the number of times a character appears in a string

eg:string A = "010203040506070809";

int k = Regex.Matches(A, @"0").Count;

The output is: 9

4. Remove the N-th character string

eg:string A = "123456789";

Now I want to delete A in 5:

 List<char> Arr = A.ToList();

Arr.RemoveAt(4);

A = new string(Arr.ToArray());

Output: 12,346,789

List 5 corresponds to the fourth element, starting from 0

5. Add the specified string to the string

string A = "1122334455";

Now I want every two characters add a space:

A = string.Join(" ", Regex.Matches(str, @"..").Cast<Match>().ToList());

The output is 1,122,334,455

Join the first argument of type string, any string will do, you need to add the corresponding string into that space

Matches first parameter is the string to be added, i.e., A; second parameter! ".." indicates every several strings, two character represents every two, three on every '.' 3;

Further, if the string is not divisible spaced character length, i.e. the length of A is not n characters of every integer multiple of n, then the last section will not be displayed

For example, the above replaced by A "112 233 445", the same characters are added every two spaces, the result is: 11223344, is not the last of a display 5

6. string taken

Take the first n characters str.Substring (0, n)
all characters str.Remove (n) after deletion of n characters
acquired from the n-th character to the last (not including the n-th) str.substring (n)
removed from the header into the first n characters, leaving the remaining str.remove (0, n)
taken from the start of the n-th substrings of length m str.substring (n, m)
begin deleting characters m from the n-th str. remove (n, m)

 

Guess you like

Origin www.cnblogs.com/zl0316/p/10931009.html