C custom string manipulation to enhance class instances
- 2021-01-18 06:36:52
- OfStack
This article illustrates the C# custom string manipulation enhancement class. Share with you for your reference. The details are as follows:
The C # # class in C free class string operations conducted on the basis of increased dramatically, we can use at ordinary times, do string manipulation in the processing of the string I think most programming are inevitable, with this class, can save you a lot of time, also can according to their own needs to extend the C # string class.
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace DotNet.Utilities
{
/// <summary>
/// String manipulation class
/// 1 , GetStrArray(string str, char speater, bool toLower) Converts a string to a delimiter List
/// 2 , GetStrArray(string str) Rotate the string In accordance with the , segmentation Change for the data
/// 3 , GetArrayStr(List list, string speater) the List Assembled according to delimiters string
/// 4 , GetArrayStr(List list) Gets a comma-delimited string for the array list
/// 5 , GetArrayValueStr(Dictionary<int, int> list) Gets a comma-delimited string for the array list
/// 6 , DelLastComma(string str) Delete the last ending 1 A comma
/// 7 , DelLastChar(string str, string strchar) Deletes the character after the last trailing specified character
/// 8 , ToSBC(string input) The function of turning the whole Angle (SBC case)
/// 9 , ToDBC(string input) The function of rotation of half angles (SBC case)
/// 10 , GetSubStringList(string o_str, char sepeater) To set a string to the specified delimiter List Remove duplicate
/// 11 , GetCleanStyle(string StrList, string SplitString) Converts a string style to a plain string
/// 12 , GetNewStyle(string StrList, string NewStyle, string SplitString, out string Error) Converts a string to a new style
/// 13 , SplitMulti(string str, string splitstr) Split string
/// 14 , SqlSafeString(string String, bool IsDel)
/// </summary>
public class StringPlus
{
/// <summary>
/// Converts a string to a delimiter List
/// </summary>
/// <param name="str"> The source string </param>
/// <param name="speater"> The 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 = str.Split(speater);
foreach (string s in ss)
{
if (!string.IsNullOrEmpty(s) && s != speater.ToString())
{
string strVal = s;
if (toLower)
{
strVal = s.ToLower();
}
list.Add(strVal);
}
}
return list;
}
/// <summary>
/// Rotate the string In accordance with the , segmentation Change for the data
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string[] GetStrArray(string str)
{
return str.Split(new Char[] { ',' });
}
/// <summary>
/// the List<string> Assembled according to delimiters string
/// </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 < list.Count; i++)
{
if (i == list.Count - 1)
{
sb.Append(list[i]);
}
else
{
sb.Append(list[i]);
sb.Append(speater);
}
}
return sb.ToString();
}
/// <summary>
/// Gets a comma-delimited string for the array list
/// </summary>
/// <param name="list"></param>
/// <returns></returns>
public static string GetArrayStr(List<int> list)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < list.Count; i++)
{
if (i == list.Count - 1)
{
sb.Append(list[i].ToString());
}
else
{
sb.Append(list[i]);
sb.Append(",");
}
}
return sb.ToString();
}
/// <summary>
/// Gets a comma-delimited string for the array list
/// </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)
{
sb.Append(kvp.Value + ",");
}
if (list.Count > 0)
{
return DelLastComma(sb.ToString());
}
else
{
return "";
}
}
#region Delete the last 1 The character after
/// <summary>
/// Delete the last ending 1 A comma
/// </summary>
public static string DelLastComma(string str)
{
return str.Substring(0, str.LastIndexOf(","));
}
/// <summary>
/// Deletes the character after the last trailing specified character
/// </summary>
public static string DelLastChar(string str, string strchar)
{
return str.Substring(0, str.LastIndexOf(strchar));
}
#endregion
/// <summary>
/// The function of turning the whole Angle (SBC case)
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string ToSBC(string input)
{
// Half Angle to Full Angle:
char[] c = input.ToCharArray();
for (int i = 0; i < c.Length; 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>
/// The function of rotation of half angles (SBC case)
/// </summary>
/// <param name="input"> The input </param>
/// <returns></returns>
public static string ToDBC(string input)
{
char[] c = input.ToCharArray();
for (int i = 0; i < c.Length; 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>
/// To set a string to the specified delimiter List Remove duplicate
/// </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 (!string.IsNullOrEmpty(s) && s != sepeater.ToString())
{
list.Add(s);
}
}
return list;
}
#region Converts a string style to a plain string
/// <summary>
/// Converts a string style to a plain 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 null, the null value is returned
if (StrList == null)
{
RetrunValue = "";
}
else
{
// Returns remove the delimiter
string NewString = "";
NewString = StrList.Replace(SplitString, "");
RetrunValue = NewString;
}
return RetrunValue;
}
#endregion
#region Converts a string to a new style
/// <summary>
/// Converts a string to a 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 a null value is entered, null is returned with an error message
if (StrList == null)
{
ReturnValue = "";
Error = " Please enter the string you want to format ";
}
else
{
// Check that the length and style of the string passed in match , If it does not match, it is an error. Give an error message and return a null value
int strListLength = StrList.Length;
int NewStyleLength = GetCleanStyle(NewStyle, SplitString).Length;
if (strListLength != NewStyleLength)
{
ReturnValue = "";
Error = " The length of the style format does not match the length of the characters entered. Please retype ";
}
else
{
// Check the position of the delimiter in the new style
string Lengstr = "";
for (int i = 0; i < NewStyle.Length; i++)
{
if (NewStyle.Substring(i, 1) == SplitString)
{
Lengstr = Lengstr + "," + i;
}
}
if (Lengstr != "")
{
Lengstr = Lengstr.Substring(1);
}
// Place the delimiter in the new style
string[] str = Lengstr.Split(',');
foreach (string bb in str)
{
StrList = StrList.Insert(int.Parse(bb), SplitString);
}
// Give the final result
ReturnValue = StrList;
// Because the output is normal, there are no errors
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.Replace("'", "");
String = String.Replace("\"", "");
return String;
}
String = String.Replace("'", "'");
String = String.Replace("\"", """);
return String;
}
#region Get the correct Id , if not a positive integer, returns 0
/// <summary>
/// Get the correct Id , if not a positive integer, returns 0
/// </summary>
/// <param name="_value"></param>
/// <returns> Returns the correct integer ID , return on failure 0</returns>
public static int StrToId(string _value)
{
if (IsNumberId(_value))
return int.Parse(_value);
else
return 0;
}
#endregion
#region check 1 Whether the string is made up of pure numbers, 1 Used for validation of query string parameters.
/// <summary>
/// check 1 Whether the string is made up of pure numbers, 1 Used for validation of query string parameters. (0 With the exception of )
/// </summary>
/// <param name="_value"> The string to validate.. </param>
/// <returns> Legal or not bool Value. </returns>
public static bool IsNumberId(string _value)
{
return QuickValidate("^[1-9]*[0-9]*$", _value);
}
#endregion
#region Quickly validate 1 Whether the string matches the specified regular expression.
/// <summary>
/// Quickly validate 1 Whether the string matches the specified regular expression.
/// </summary>
/// <param name="_express"> The content of the regular expression. </param>
/// <param name="_value"> The string to validate. </param>
/// <returns> Legal or not bool Value. </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 myRegex.IsMatch(_value);
}
#endregion
}
}
I hope this article will be helpful to your C# programming.