String manipulation is the most basic, most common, and most commonly used in C #, and I've summarized several common methods below.
1. Convert strings to List by delimiter
/// <summary> ///Convert a string to a List by delimiter/// </summary> /// <param name= "str" >Source String</param> /// <param name= "Speater" >Separators</param> /// <param name= "ToLower" >Convert to lowercase</param> /// <returns></returns> Public Staticlist<string> Getstrarray (stringStrCharSpeater,BOOLtoLower) { varList =Newlist<string>(); varSS =Str. Split (Speater); foreach(varSinchSS) { if(string. Isnullorwhitespace (s) | | s = = Speater. ToString (CultureInfo.InvariantCulture))Continue; varStrval =s; if(toLower) {Strval=S.tolower (); } list. ADD (Strval); } returnlist; }
2. Change the string to the data
/// <summary> ///Turn a string into a data partition/// </summary> /// <param name= "str" ></param> /// <returns></returns> Public Static string[] Getstrarray (stringstr) { returnStr. Split (New[] {',',',' }); }
3. Get the comma-delimited string for the array list
/// <summary> ///get a comma-delimited string of array lists/// </summary> /// <param name= "list" ></param> /// <param name= "Speater" ></param> /// <returns></returns> Public Static stringGetarraystr (list<string> list,stringspeater) { varSB =NewStringBuilder (); for(vari =0; I < list. Count; i++) { if(i = = list.) Count-1) {sb. Append (List[i]); } Else{sb. Append (List[i]); Sb. Append (Speater); } } returnsb. ToString (); }
4. Get the string length
/// <summary> ///Get string length/// </summary> /// <param name= "Inputstr" ></param> /// <returns></returns> Public Static intStrlength (stringinputstr) { varASC =Newasciiencoding (); vars =ASC. GetBytes (INPUTSTR); returnS.sum (t = = T = = the?2:1); }
5. Intercept a specified length string
/// <summary> ///intercepts a specified length string/// </summary> /// <param name= "inputstring" ></param> /// <param name= "Len" ></param> /// <returns></returns> Public Static stringCUTSTR (stringInputString,intLen) { varIsshowfix =false; if(Len%2==1) {Isshowfix=true; Len--; } varASCII =Newasciiencoding (); varTemplen =0; varTempString =""; vars =ASCII. GetBytes (inputstring); for(vari =0; i < s.length; i++) {Templen+ = S[i] = = the?2:1; Try{tempstring+ = Inputstring.substring (i,1); } Catch { Break; } if(Templen >len) Break; } varMyByte =Encoding.Default.GetBytes (inputstring); if(Isshowfix && MyByte. Length >len) tempstring+="... .."; returntempstring; }
For the time being, there are five ways.
< experience >c#/. NET string manipulation methods Summary