The interception of strings commonly used in C #
In development we often need to intercept strings, many beginners, do not know how to do a good interception of strings, the following is the process of intercepting strings we must know the following functions: substring function, Remove function, indexof function.
substring function
Returns a substring of the first parameter, starting at the position specified by the second argument, and the length specified by the third argument.
If you do not specify a third argument, the substring that starts at the position specified by the second argument until the end of the string is returned.
Remove function
Remove (int ch, int fromIndex) removes the string from the beginning of CH to the FromIndex.
indexOf function
int indexOf (int ch) returns the index of the specified character at the first occurrence of this string.
int indexOf (int ch, int fromIndex) searches starting at the specified index, returning the index at the first occurrence of the specified character in this string.
int indexOf (String str) returns the index of the specified substring that first appears in this string.
int indexOf (String str, int fromIndex) starts at the specified index and returns the index of the specified substring that first appears in this string.
String str= "123abc456";
int i=3;
1 Take the first I character of a string
Str=str. Substring (0,i); or STR=STR. Remove (i,str. LENGTH-I);
2 Remove the first I character of a string:
Str=str. Remove (0,i); or STR=STR. Substring (i);
3 Take I characters starting from the right:
Str=str. Substring (str. LENGTH-I); or STR=STR. Remove (0,str. LENGTH-I);
4 Remove the I characters starting from the right:
Str=str. Substring (0,str. LENGTH-I); or STR=STR. Remove (str. Length-i,i);
5 Determine if there is "ABC" in the string and remove it
Using System.Text.RegularExpressions;
String str = "123abc456";
String a= "abc";
Regex r = new Regex (a);
Match m = r.match (str);
if (m.success)
{
Take one of the following two.
Str=str. Replace (A, "");
Response.Write (str);
String str1,str2;
Str1=str. Substring (0,m.index);
Str2=str. Substring (m.index+a.length,str. Length-a.length-m.index);
Response.Write (STR1+STR2);
}
6 If "a" in the string is replaced with "a"
Str=str. Replace ("A", "a");
String str= "Adcdef"; int indexstart = str. IndexOf ("D");
int EndIndex =str. IndexOf ("E");
string tostr = str. SubString (Indexstart,endindex-indexstart);
C # intercepts the question of the last character of a string!
Str1. Substring (str1. LastIndexOf (",") +1);
C # intercepts the last character of a string
K = K.substring (k.length-1, 1);
C # intercepts strings