This article mainly introduces the use of C # string interception function, the example summarizes the use of substring,remove,indexof and other functions, and the specific application of the example analysis, the need for friends can refer to the following
This paper summarizes the common use of string interception function in C #. Share to everyone for your reference. The specific analysis is as follows:
In C #, the string intercept function includes the SUBSTRING function, the Remove function, the IndexOf function, and all three of them can intercept the string, let's take a look at each one.
Here are the following functions that we must know in the process of intercepting a string: The substring function, the Remove function, and the 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.
The 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.
Cases:
The code is as follows:
string str="123abc456"; int i=3;
1 Take the first I character of a string
The code is as follows:
Str=str. Substring (0// or STR=STR. Remove (i,str. LENGTH-I);
2 Remove the first I character of a string:
The code is as follows:
Str=str. Remove (0// or STR=STR. Substring (i);
3 Take I characters starting from the right:
The code is as follows:
// or STR=STR. Remove (0,str. LENGTH-I);
4 Remove the I characters starting from the right:
The code is as follows:
Str=str. Substring (0// or STR=STR. Remove (str. Length-i,i);
5 Determine if there is "ABC" in the string and remove it
The code is as follows:
usingSystem.Text.RegularExpressions; stringstr ="123abc456"; stringA="ABC"; Regex R=NewRegex (a); Match m=R.match (str); if(m.success) {//take one of the following two. Str=str. Replace (A,""); Response.Write (str); stringstr1,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"
The code is as follows:
Str=str. Replace ("a","A");stringStr="Adcdef";intIndexstart = str. IndexOf ("D"); intEndIndex =str. IndexOf ("e"); stringTostr = str. SubString (Indexstart,endindex-indexstart);
C # intercepts the question of the last character of a string!
The code is as follows:
Str1. Substring (str1. LastIndexOf (",") +1);
C # intercepts the last character of a string
The code is as follows:
K = k.substring (k.length-11);
Add:
Chinese string intercept function
The code is as follows:
///Str_value characters///Str_len The length of the character to intercept Public stringLEFTX (stringStr_value,intStr_len) { intP_num =0; inti; stringNew_str_value =""; if(str_value=="") {New_str_value=""; } Else { intLen_num =Str_value. Length; //if (Len_num < Str_len)//{ //Str_len = Len_num; //} for(i =0; I<=len_num-1; i++) { //Str_value. Substring (i,1); if(I >len_num) Break; Charc = Convert.tochar (Str_value. Substring (I,1)); if(((int) C >255) || ((int) c<0) ) {P_num= P_num +2; } Else{p_num= P_num +1; } if(P_num >=Str_len) {New_str_value= Str_value. Substring (0, i+1); Break; } Else{New_str_value=Str_value; } } } returnNew_str_value;}
I hope this article is helpful to everyone's C # programming.
Summary of truncation function usage for C # string