This article describes the C# custom string operation enhancement class. Share it for your reference. The details are as follows:
This C# class has greatly enhanced the string operation class based on the C# free string operation class, and has done all the string operations we may use in daily life. I think most of the programming of string processing is inevitable. With this class, you can save a lot of time. At the same time, you can expand this C# string class according to your needs.
using System; using ; using ; using ; namespace { /// <summary> /// String operation class /// 1. GetStrArray(string str, char speaker, bool toLower) Convert strings to List by delimiter /// 2. GetStrArray(string str) converts the string according to, splits and replaces it with data /// 3. GetArrayStr(List list, string speech) Assemble List into string according to the separator /// 4. GetArrayStr(List list) Get comma-separated strings /// 5. GetArrayValueStr(Dictionary<int, int> list) to get the string separated by commas /// 6. DelLastComma(string str) deletes a comma at the end /// 7. DelLastChar(string str, string strchar) deletes the character after the last specified character /// 8. ToSBC (string input) to full-width function (SBC case) /// 9. ToDBC(string input) function to turn half a corner (SBC case) /// 10. GetSubStringList(string o_str, char sepeater) Install the string into List according to the specified separator to remove duplication /// 11. GetCleanStyle(string StrList, string SplitString) converts string style to pure string /// 12. GetNewStyle(string StrList, string NewStyle, string SplitString, out string Error) Convert string to new style /// 13. SplitMulti(string str, string splittr) split string /// 14、SqlSafeString(string String, bool IsDel) /// </summary> public class StringPlus { /// <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) { List<string> list = new List<string>(); string[] ss = (speater); foreach (string s in ss) { if (!(s) && s != ()) { string strVal = s; if (toLower) { strVal = (); } (strVal); } } return list; } /// <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 Char[] { ',' }); } /// <summary> /// Assemble List<string> into string according to the separator /// </summary> /// <param name="list"></param> /// <param name="speater"></param> /// <returns></returns> public static string GetArrayStr(List<string> list, string speater) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < ; i++) { if (i == - 1) { (list[i]); } else { (list[i]); (speater); } } return (); } /// <summary> /// Get a comma-separated string /// </summary> /// <param name="list"></param> /// <returns></returns> public static string GetArrayStr(List<int> list) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < ; i++) { if (i == - 1) { (list[i].ToString()); } else { (list[i]); (","); } } return (); } /// <summary> /// Get a comma-separated string /// </summary> /// <param name="list"></param> /// <returns></returns> public static string GetArrayValueStr(Dictionary<int, int> list) { StringBuilder sb = new StringBuilder(); foreach (KeyValuePair<int, int> kvp in list) { ( + ","); } if ( > 0) { return DelLastComma(()); } else { return ""; } } #region Delete the character after the last character /// <summary> /// Delete a comma at the end /// </summary> public static string DelLastComma(string str) { return (0, (",")); } /// <summary> /// Delete the characters after the last specified character at the end /// </summary> public static string DelLastChar(string str, string strchar) { return (0, (strchar)); } #endregion /// <summary> /// Full-width function (SBC case) /// </summary> /// <param name="input"></param> /// <returns></returns> public static string ToSBC(string input) { //Turn the full-width: char[] c = (); for (int i = 0; i < ; i++) { if (c[i] == 32) { c[i] = (char)12288; continue; } if (c[i] < 127) c[i] = (char)(c[i] + 65248); } return new string(c); } /// <summary> /// Function of half-width (SBC case) /// </summary> /// <param name="input">Input</param> /// <returns></returns> public static string ToDBC(string input) { char[] c = (); for (int i = 0; i < ; i++) { if (c[i] == 12288) { c[i] = (char)32; continue; } if (c[i] > 65280 && c[i] < 65375) c[i] = (char)(c[i] - 65248); } return new string(c); } /// <summary> /// Contain the string into List as specified delimiter to remove duplication /// </summary> /// <param name="o_str"></param> /// <param name="sepeater"></param> /// <returns></returns> public static List<string> GetSubStringList(string o_str, char sepeater) { List<string> list = new List<string>(); string[] ss = o_str.Split(sepeater); foreach (string s in ss) { if (!(s) && s != ()) { (s); } } return list; } #region Convert string style to pure string /// <summary> /// Convert string style to pure string /// </summary> /// <param name="StrList"></param> /// <param name="SplitString"></param> /// <returns></returns> public static string GetCleanStyle(string StrList, string SplitString) { string RetrunValue = ""; //If it is empty, return the empty value if (StrList == null) { RetrunValue = ""; } else { //Return to remove the separator string NewString = ""; NewString = (SplitString, ""); RetrunValue = NewString; } return RetrunValue; } #endregion #region Convert string to new style /// <summary> /// Convert string to new style /// </summary> /// <param name="StrList"></param> /// <param name="NewStyle"></param> /// <param name="SplitString"></param> /// <param name="Error"></param> /// <returns></returns> public static string GetNewStyle(string StrList, string NewStyle, string SplitString, out string Error) { string ReturnValue = ""; //If you enter a null value, return empty and give an error message if (StrList == null) { ReturnValue = ""; Error = "Please enter the string that needs to be divided into formats"; } else { //Check whether the passed string length and style match. If it does not match, it means that the use is incorrect. Give an error message and return a null value int strListLength = ; int NewStyleLength = GetCleanStyle(NewStyle, SplitString).Length; if (strListLength != NewStyleLength) { ReturnValue = ""; Error = "The length of the style format does not match the length of the entered character, please re-enter"; } else { //Check the position of the separator in the new style string Lengstr = ""; for (int i = 0; i < ; i++) { if ((i, 1) == SplitString) { Lengstr = Lengstr + "," + i; } } if (Lengstr != "") { Lengstr = (1); } //Place the separator in the new style string[] str = (','); foreach (string bb in str) { StrList = ((bb), SplitString); } //Give the final result ReturnValue = StrList; //Because it is normal output, there is no error Error = ""; } } return ReturnValue; } #endregion /// <summary> ///Split string /// </summary> /// <param name="str"></param> /// <param name="splitstr"></param> /// <returns></returns> public static string[] SplitMulti(string str, string splitstr) { string[] strArray = null; if ((str != null) && (str != "")) { strArray = new Regex(splitstr).Split(str); } return strArray; } public static string SqlSafeString(string String, bool IsDel) { if (IsDel) { String = ("'", ""); String = ("\"", ""); return String; } String = ("'", "&#39;"); String = ("\"", "&#34;"); return String; } #region Gets the correct Id, if it is not a positive integer, return 0 /// <summary> /// Get the correct ID, if it is not a positive integer, return 0 /// </summary> /// <param name="_value"></param> /// <returns>Returns the correct integer ID, return 0 if failed</returns> public static int StrToId(string _value) { if (IsNumberId(_value)) return (_value); else return 0; } #endregion #region Checks whether a string is purely composed of numbers, which is generally used to query the validity verification of string parameters. /// <summary> /// Check whether a string is purely composed of numbers, which is generally used to query the validity verification of string parameters. (Except 0) /// </summary> /// <param name="_value">Stand to be verified. . </param> /// <returns> Whether the bool value is legal. </returns> public static bool IsNumberId(string _value) { return QuickValidate("^[1-9]*[0-9]*$", _value); } #endregion #region Quickly verify that a string complies with the specified regular expression. /// <summary> /// Quickly verify that a string complies with the specified regular expression. /// </summary> /// <param name="_express">Content of regular expression. </param> /// <param name="_value">Stand to be verified. </param> /// <returns> Whether the bool value is legal. </returns> public static bool QuickValidate(string _express, string _value) { if (_value == null) return false; Regex myRegex = new Regex(_express); if (_value.Length == 0) { return false; } return (_value); } #endregion } }
I hope this article will be helpful to everyone's C# programming.