C#自界說的字符串操作加強類實例。本站提示廣大學習愛好者:(C#自界說的字符串操作加強類實例)文章只能為提供參考,不一定能成為您想要的結果。以下是C#自界說的字符串操作加強類實例正文
本文實例講述了C#自界說的字符串操作加強類。分享給年夜家供年夜家參考。詳細以下:
這個C#類在C#自在的字符串操作類的基本長進行的年夜幅度加強,把我們日常平凡能夠用到的字符串操作都做出來了,字符串的處置我想年夜部門編程都弗成防止,有了這個類,可以節儉你許多時光,同時可以依據本身的須要對這個C#字符串類停止擴大。
using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; namespace DotNet.Utilities { /// <summary> /// 字符串操作類 /// 1、GetStrArray(string str, char speater, bool toLower) 把字符串依照分隔符轉換成 List /// 2、GetStrArray(string str) 把字符串轉 依照, 朋分 換為數據 /// 3、GetArrayStr(List list, string speater) 把 List 依照分隔符組裝成 string /// 4、GetArrayStr(List list) 獲得數組列表以逗號分隔的字符串 /// 5、GetArrayValueStr(Dictionary<int, int> list)獲得數組列表以逗號分隔的字符串 /// 6、DelLastComma(string str)刪除最初開頭的一個逗號 /// 7、DelLastChar(string str, string strchar)刪除最初開頭的指定字符後的字符 /// 8、ToSBC(string input)轉全角的函數(SBC case) /// 9、ToDBC(string input)轉半角的函數(SBC case) /// 10、GetSubStringList(string o_str, char sepeater)把字符串依照指定分隔符裝成 List 去除反復 /// 11、GetCleanStyle(string StrList, string SplitString)將字符串款式轉換為純字符串 /// 12、GetNewStyle(string StrList, string NewStyle, string SplitString, out string Error)將字符串轉換為新款式 /// 13、SplitMulti(string str, string splitstr)朋分字符串 /// 14、SqlSafeString(string String, bool IsDel) /// </summary> public class StringPlus { /// <summary> /// 把字符串依照分隔符轉換成 List /// </summary> /// <param name="str">源字符串</param> /// <param name="speater">分隔符</param> /// <param name="toLower">能否轉換為小寫</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> /// 把字符串轉 依照, 朋分 換為數據 /// </summary> /// <param name="str"></param> /// <returns></returns> public static string[] GetStrArray(string str) { return str.Split(new Char[] { ',' }); } /// <summary> /// 把 List<string> 依照分隔符組裝成 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> /// 獲得數組列表以逗號分隔的字符串 /// </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> /// 獲得數組列表以逗號分隔的字符串 /// </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 刪除最初一個字符以後的字符 /// <summary> /// 刪除最初開頭的一個逗號 /// </summary> public static string DelLastComma(string str) { return str.Substring(0, str.LastIndexOf(",")); } /// <summary> /// 刪除最初開頭的指定字符後的字符 /// </summary> public static string DelLastChar(string str, string strchar) { return str.Substring(0, str.LastIndexOf(strchar)); } #endregion /// <summary> /// 轉全角的函數(SBC case) /// </summary> /// <param name="input"></param> /// <returns></returns> public static string ToSBC(string input) { //半角轉全角: 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> /// 轉半角的函數(SBC case) /// </summary> /// <param name="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> /// 把字符串依照指定分隔符裝成 List 去除反復 /// </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 將字符串款式轉換為純字符串 /// <summary> /// 將字符串款式轉換為純字符串 /// </summary> /// <param name="StrList"></param> /// <param name="SplitString"></param> /// <returns></returns> public static string GetCleanStyle(string StrList, string SplitString) { string RetrunValue = ""; //假如為空,前往空值 if (StrList == null) { RetrunValue = ""; } else { //前往去失落分隔符 string NewString = ""; NewString = StrList.WordStr(SplitString, ""); RetrunValue = NewString; } return RetrunValue; } #endregion #region 將字符串轉換為新款式 /// <summary> /// 將字符串轉換為新款式 /// </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 (StrList == null) { ReturnValue = ""; Error = "請輸出須要劃分格局的字符串"; } else { //檢討傳入的字符串長度和款式能否婚配,假如不婚配,則解釋應用毛病。給失足誤信息並前往空值 int strListLength = StrList.Length; int NewStyleLength = GetCleanStyle(NewStyle, SplitString).Length; if (strListLength != NewStyleLength) { ReturnValue = ""; Error = "款式格局的長度與輸出的字符長度不符,請從新輸出"; } else { //檢討新款式平分隔符的地位 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); } //將分隔符放在新款式中的地位 string[] str = Lengstr.Split(','); foreach (string bb in str) { StrList = StrList.Insert(int.Parse(bb), SplitString); } //給出最初的成果 ReturnValue = StrList; //由於是正常的輸入,沒有毛病 Error = ""; } } return ReturnValue; } #endregion /// <summary> /// 朋分字符串 /// </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.WordStr("'", ""); String = String.WordStr("\"", ""); return String; } String = String.WordStr("'", "'"); String = String.WordStr("\"", """); return String; } #region 獲得准確的Id,假如不是正整數,前往0 /// <summary> /// 獲得准確的Id,假如不是正整數,前往0 /// </summary> /// <param name="_value"></param> /// <returns>前往准確的整數ID,掉敗前往0</returns> public static int StrToId(string _value) { if (IsNumberId(_value)) return int.Parse(_value); else return 0; } #endregion #region 檢討一個字符串能否是純數字組成的,普通用於查詢字符串參數的有用性驗證。 /// <summary> /// 檢討一個字符串能否是純數字組成的,普通用於查詢字符串參數的有用性驗證。(0除外) /// </summary> /// <param name="_value">需驗證的字符串。。</param> /// <returns>能否正當的bool值。</returns> public static bool IsNumberId(string _value) { return QuickValidate("^[1-9]*[0-9]*$", _value); } #endregion #region 疾速驗證一個字符串能否相符指定的正則表達式。 /// <summary> /// 疾速驗證一個字符串能否相符指定的正則表達式。 /// </summary> /// <param name="_express">正則表達式的內容。</param> /// <param name="_value">需驗證的字符串。</param> /// <returns>能否正當的bool值。</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 } }
願望本文所述對年夜家的C#法式設計有所贊助。