SoFunction
Updated on 2025-03-08

C# custom string operation enhancement class instance

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)
  /// &lt;/summary&gt;
  public class StringPlus
  {
    /// &lt;summary&gt;
    /// Convert strings into List by separator    /// &lt;/summary&gt;
    /// <param name="str">Source string</param>    /// <param name="speater">separator</param>    /// <param name="toLower">Whether to convert to lowercase</param>    /// &lt;returns&gt;&lt;/returns&gt;
    public static List&lt;string&gt; GetStrArray(string str, char speater, bool toLower)
    {
      List&lt;string&gt; list = new List&lt;string&gt;();
      string[] ss = (speater);
      foreach (string s in ss)
      {
        if (!(s) &amp;&amp; s != ())
        {
          string strVal = s;
          if (toLower)
          {
            strVal = ();
          }
          (strVal);
        }
      }
      return list;
    }
    /// &lt;summary&gt;
    /// Transform the string according to, split and replace it with data    /// &lt;/summary&gt;
    /// &lt;param name="str"&gt;&lt;/param&gt;
    /// &lt;returns&gt;&lt;/returns&gt;
    public static string[] GetStrArray(string str)
    {
      return (new Char[] { ',' });
    }
    /// &lt;summary&gt;
    /// Assemble List<string> into string according to the separator    /// &lt;/summary&gt;
    /// &lt;param name="list"&gt;&lt;/param&gt;
    /// &lt;param name="speater"&gt;&lt;/param&gt;
    /// &lt;returns&gt;&lt;/returns&gt;
    public static string GetArrayStr(List&lt;string&gt; list, string speater)
    {
      StringBuilder sb = new StringBuilder();
      for (int i = 0; i &lt; ; i++)
      {
        if (i ==  - 1)
        {
          (list[i]);
        }
        else
        {
          (list[i]);
          (speater);
        }
      }
      return ();
    }
    /// &lt;summary&gt;
    /// Get a comma-separated string    /// &lt;/summary&gt;
    /// &lt;param name="list"&gt;&lt;/param&gt;
    /// &lt;returns&gt;&lt;/returns&gt;
    public static string GetArrayStr(List&lt;int&gt; list)
    {
      StringBuilder sb = new StringBuilder();
      for (int i = 0; i &lt; ; i++)
      {
        if (i ==  - 1)
        {
          (list[i].ToString());
        }
        else
        {
          (list[i]);
          (",");
        }
      }
      return ();
    }
    /// &lt;summary&gt;
    /// Get a comma-separated string    /// &lt;/summary&gt;
    /// &lt;param name="list"&gt;&lt;/param&gt;
    /// &lt;returns&gt;&lt;/returns&gt;
    public static string GetArrayValueStr(Dictionary&lt;int, int&gt; list)
    {
      StringBuilder sb = new StringBuilder();
      foreach (KeyValuePair&lt;int, int&gt; kvp in list)
      {
        ( + ",");
      }
      if ( &gt; 0)
      {
        return DelLastComma(());
      }
      else
      {
        return "";
      }
    }
    #region Delete the character after the last character    /// &lt;summary&gt;
    /// Delete a comma at the end    /// &lt;/summary&gt;
    public static string DelLastComma(string str)
    {
      return (0, (","));
    }
    /// &lt;summary&gt;
    /// Delete the characters after the last specified character at the end    /// &lt;/summary&gt;
    public static string DelLastChar(string str, string strchar)
    {
      return (0, (strchar));
    }
    #endregion
    /// &lt;summary&gt;
    /// Full-width function (SBC case)    /// &lt;/summary&gt;
    /// &lt;param name="input"&gt;&lt;/param&gt;
    /// &lt;returns&gt;&lt;/returns&gt;
    public static string ToSBC(string input)
    {
      //Turn the full-width:      char[] c = ();
      for (int i = 0; i &lt; ; i++)
      {
        if (c[i] == 32)
        {
          c[i] = (char)12288;
          continue;
        }
        if (c[i] &lt; 127)
          c[i] = (char)(c[i] + 65248);
      }
      return new string(c);
    }
    /// &lt;summary&gt;
    /// Function of half-width (SBC case)    /// &lt;/summary&gt;
    /// <param name="input">Input</param>    /// &lt;returns&gt;&lt;/returns&gt;
    public static string ToDBC(string input)
    {
      char[] c = ();
      for (int i = 0; i &lt; ; i++)
      {
        if (c[i] == 12288)
        {
          c[i] = (char)32;
          continue;
        }
        if (c[i] &gt; 65280 &amp;&amp; c[i] &lt; 65375)
          c[i] = (char)(c[i] - 65248);
      }
      return new string(c);
    }
    /// &lt;summary&gt;
    /// Contain the string into List as specified delimiter to remove duplication    /// &lt;/summary&gt;
    /// &lt;param name="o_str"&gt;&lt;/param&gt;
    /// &lt;param name="sepeater"&gt;&lt;/param&gt;
    /// &lt;returns&gt;&lt;/returns&gt;
    public static List&lt;string&gt; GetSubStringList(string o_str, char sepeater)
    {
      List&lt;string&gt; list = new List&lt;string&gt;();
      string[] ss = o_str.Split(sepeater);
      foreach (string s in ss)
      {
        if (!(s) &amp;&amp; s != ())
        {
          (s);
        }
      }
      return list;
    }
    #region Convert string style to pure string    /// &lt;summary&gt;
    /// Convert string style to pure string    /// &lt;/summary&gt;
    /// &lt;param name="StrList"&gt;&lt;/param&gt;
    /// &lt;param name="SplitString"&gt;&lt;/param&gt;
    /// &lt;returns&gt;&lt;/returns&gt;
    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    /// &lt;summary&gt;
    /// Convert string to new style    /// &lt;/summary&gt;
    /// &lt;param name="StrList"&gt;&lt;/param&gt;
    /// &lt;param name="NewStyle"&gt;&lt;/param&gt;
    /// &lt;param name="SplitString"&gt;&lt;/param&gt;
    /// &lt;param name="Error"&gt;&lt;/param&gt;
    /// &lt;returns&gt;&lt;/returns&gt;
    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 &lt; ; 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
    /// &lt;summary&gt;
    ///Split string    /// &lt;/summary&gt;
    /// &lt;param name="str"&gt;&lt;/param&gt;
    /// &lt;param name="splitstr"&gt;&lt;/param&gt;
    /// &lt;returns&gt;&lt;/returns&gt;
    public static string[] SplitMulti(string str, string splitstr)
    {
      string[] strArray = null;
      if ((str != null) &amp;&amp; (str != ""))
      {
        strArray = new Regex(splitstr).Split(str);
      }
      return strArray;
    }
    public static string SqlSafeString(string String, bool IsDel)
    {
      if (IsDel)
      {
        String = ("'", "");
        String = ("\"", "");
        return String;
      }
      String = ("'", "&amp;#39;");
      String = ("\"", "&amp;#34;");
      return String;
    }
    #region Gets the correct Id, if it is not a positive integer, return 0    /// &lt;summary&gt;
    /// Get the correct ID, if it is not a positive integer, return 0    /// &lt;/summary&gt;
    /// &lt;param name="_value"&gt;&lt;/param&gt;
    /// <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.    /// &lt;summary&gt;
    /// Check whether a string is purely composed of numbers, which is generally used to query the validity verification of string parameters.  (Except 0)    /// &lt;/summary&gt;
    /// <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.    /// &lt;summary&gt;
    /// Quickly verify that a string complies with the specified regular expression.    /// &lt;/summary&gt;
    /// <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.