String operations are the most basic, common and most commonly used in C#. I have summarized several common methods below:
1. Convert strings to List by separator
/// <summary> /// Convert strings into List by separator /// </summary> /// <param name="str">Source string</param> /// <param name="speater">separator</param> /// <param name="toLower">Whether to convert to lowercase</param> /// <returns></returns> public static List<string> GetStrArray(string str, char speater, bool toLower) { var list = new List<string>(); var ss = (speater); foreach (var s in ss) { if ((s) || s == ()) continue; var strVal = s; if (toLower) { strVal = (); } (strVal); } return list; }
2. Turn the string according to, split and replace it with data
/// <summary> /// Transform the string according to, split and replace it with data /// </summary> /// <param name="str"></param> /// <returns></returns> public static string[] GetStrArray(string str) { return (new[] { ',', ',' }); }
3. Get a comma-separated string
/// <summary> /// Get a comma-separated string /// </summary> /// <param name="list"></param> /// <param name="speater"></param> /// <returns></returns> public static string GetArrayStr(List<string> list, string speater) { var sb = new StringBuilder(); for (var i = 0; i < ; i++) { if (i == - 1) { (list[i]); } else { (list[i]); (speater); } } return (); }
4. Get the string length
/// <summary> /// Get the string length /// </summary> /// <param name="inputStr"></param> /// <returns></returns> public static int StrLength(string inputStr) { var asc = new ASCIIEncoding(); var s = (inputStr); return (t => t == 63 ? 2 : 1); }
5. Intercept the string of specified length
/// <summary> /// Intercept the string of specified length /// </summary> /// <param name="inputString"></param> /// <param name="len"></param> /// <returns></returns> public static string CutStr(string inputString, int len) { var isShowFix = false; if (len % 2 == 1) { isShowFix = true; len--; } var ascii = new ASCIIEncoding(); var tempLen = 0; var tempString = ""; var s = (inputString); for (var i = 0; i < ; i++) { tempLen += s[i] == 63 ? 2 : 1; try { tempString += (i, 1); } catch { break; } if (tempLen > len) break; } var mybyte = (inputString); if (isShowFix && > len) tempString += "…"; return tempString; }
There are only these five methods for the time being.