This article mainly introduces the summary of the 5 C # string manipulation methods to share, this article explained that the string to be converted to List by separator, the string to follow, split into data, get array list comma-delimited string, get string length and other methods, need friends can refer to the
String manipulation is the most basic, most common, and most commonly used in C #, and I've summarized several common methods here:
1. Convert string to List by separator
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22-23 |
<summary>///converts strings to List///</summary>///<param name= "str" > Source string </param>///<par Am Name= "Speater" > Separator </param>///<param name= "ToLower" > Convert to lowercase </param>///<returns>< /returns> public static list<string> Getstrarray (String str, char Speater, bool toLower) {var List = new List< ;string> (); var ss = Str. Split (Speater); foreach (var s in ss) {if (string). Isnullorwhitespace (s) | | s = = Speater. ToString (CultureInfo.InvariantCulture)) continue; var strval = s; if (toLower) {strval = S.tolower ();} list. ADD (Strval); } return list; } |
2. The string to follow, split into data
?
1 2 3 4 5 6 7 8 9 |
<summary>///the string to follow, split into data///</summary>///<param name= "str" ></param>///<returns& gt;</returns> public static string[] Getstrarray (String str) {return str. Split (new[] {', ', ', '}); } |
3. Get array list comma-delimited string
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22-23 |
<summary>///get array list comma-delimited string///</summary>///<param name= "list" ></param>///<param na Me= "Speater" ></param>///<returns></returns> public static string Getarraystr (list<string > list, String speater) {var sb = new StringBuilder (); for (var i = 0; i < list.) Count; i++) {if (i = = list. Count-1) {sb. Append (List[i]); } else {sb. Append (List[i]); Sb. Append (Speater); } return SB. ToString (); } |
4. Get string length
?
1 2 3 4 5 6 7 8 9 10 11 |
<summary>///Get string length///</summary>///<param name= "inputstr" ></param>///<returns> </returns> public static int strlength (string inputstr) {var asc = new ASCIIEncoding (); var s = asc. GetBytes (INPUTSTR); Return S.sum (t => t = = 63? 2:1); } |
5. Intercept the specified length string
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 This is the |
|
These five methods are temporary.