您好,登錄后才能下訂單哦!
一個(gè)非常好的C#字符串操作處理類StringHelper.cs,具體內(nèi)容如下
/// <summary> /// 類說明:Assistant /// 編 碼 人:蘇飛 /// 聯(lián)系方式:361983679 /// 更新網(wǎng)站:http://www.sufeinet.com/thread-655-1-1.html /// </summary> 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) 把字符串按照分隔符轉(zhuǎn)換成 List /// 2、GetStrArray(string str) 把字符串轉(zhuǎn) 按照, 分割 換為數(shù)據(jù) /// 3、GetArrayStr(List list, string speater) 把 List 按照分隔符組裝成 string /// 4、GetArrayStr(List list) 得到數(shù)組列表以逗號(hào)分隔的字符串 /// 5、GetArrayValueStr(Dictionary<int, int> list)得到數(shù)組列表以逗號(hào)分隔的字符串 /// 6、DelLastComma(string str)刪除最后結(jié)尾的一個(gè)逗號(hào) /// 7、DelLastChar(string str, string strchar)刪除最后結(jié)尾的指定字符后的字符 /// 8、ToSBC(string input)轉(zhuǎn)全角的函數(shù)(SBC case) /// 9、ToDBC(string input)轉(zhuǎn)半角的函數(shù)(SBC case) /// 10、GetSubStringList(string o_str, char sepeater)把字符串按照指定分隔符裝成 List 去除重復(fù) /// 11、GetCleanStyle(string StrList, string SplitString)將字符串樣式轉(zhuǎn)換為純字符串 /// 12、GetNewStyle(string StrList, string NewStyle, string SplitString, out string Error)將字符串轉(zhuǎn)換為新樣式 /// 13、SplitMulti(string str, string splitstr)分割字符串 /// 14、SqlSafeString(string String, bool IsDel) /// </summary> public class StringHelper { /// <summary> /// 把字符串按照分隔符轉(zhuǎn)換成 List /// </summary> /// <param name="str">源字符串</param> /// <param name="speater">分隔符</param> /// <param name="toLower">是否轉(zhuǎn)換為小寫</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> /// 把字符串轉(zhuǎn) 按照, 分割 換為數(shù)據(jù) /// </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> /// 得到數(shù)組列表以逗號(hào)分隔的字符串 /// </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> /// 得到數(shù)組列表以逗號(hào)分隔的字符串 /// </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 刪除最后一個(gè)字符之后的字符 /// <summary> /// 刪除最后結(jié)尾的一個(gè)逗號(hào) /// </summary> public static string DelLastComma(string str) { return str.Substring(0, str.LastIndexOf(",")); } /// <summary> /// 刪除最后結(jié)尾的指定字符后的字符 /// </summary> public static string DelLastChar(string str, string strchar) { return str.Substring(0, str.LastIndexOf(strchar)); } #endregion /// <summary> /// 轉(zhuǎn)全角的函數(shù)(SBC case) /// </summary> /// <param name="input"></param> /// <returns></returns> public static string ToSBC(string input) { //半角轉(zhuǎn)全角: 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> /// 轉(zhuǎn)半角的函數(shù)(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 去除重復(fù) /// </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 將字符串樣式轉(zhuǎn)換為純字符串 /// <summary> /// 將字符串樣式轉(zhuǎn)換為純字符串 /// </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.Replace(SplitString, ""); RetrunValue = NewString; } return RetrunValue; } #endregion #region 將字符串轉(zhuǎn)換為新樣式 /// <summary> /// 將字符串轉(zhuǎn)換為新樣式 /// </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 = ""; //如果輸入空值,返回空,并給出錯(cuò)誤提示 if (StrList == null) { ReturnValue = ""; Error = "請(qǐng)輸入需要?jiǎng)澐指袷降淖址?; } else { //檢查傳入的字符串長(zhǎng)度和樣式是否匹配,如果不匹配,則說明使用錯(cuò)誤。給出錯(cuò)誤信息并返回空值 int strListLength = StrList.Length; int NewStyleLength = GetCleanStyle(NewStyle, SplitString).Length; if (strListLength != NewStyleLength) { ReturnValue = ""; Error = "樣式格式的長(zhǎng)度與輸入的字符長(zhǎng)度不符,請(qǐng)重新輸入"; } 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); } //給出最后的結(jié)果 ReturnValue = StrList; //因?yàn)槭钦5妮敵?,沒有錯(cuò)誤 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.Replace("'", ""); String = String.Replace("\"", ""); return String; } String = String.Replace("'", "'"); String = String.Replace("\"", """); return String; } #region 獲取正確的Id,如果不是正整數(shù),返回0 /// <summary> /// 獲取正確的Id,如果不是正整數(shù),返回0 /// </summary> /// <param name="_value"></param> /// <returns>返回正確的整數(shù)ID,失敗返回0</returns> public static int StrToId(string _value) { if (IsNumberId(_value)) return int.Parse(_value); else return 0; } #endregion #region 檢查一個(gè)字符串是否是純數(shù)字構(gòu)成的,一般用于查詢字符串參數(shù)的有效性驗(yàn)證。 /// <summary> /// 檢查一個(gè)字符串是否是純數(shù)字構(gòu)成的,一般用于查詢字符串參數(shù)的有效性驗(yàn)證。(0除外) /// </summary> /// <param name="_value">需驗(yàn)證的字符串。。</param> /// <returns>是否合法的bool值。</returns> public static bool IsNumberId(string _value) { return QuickValidate("^[1-9]*[0-9]*$", _value); } #endregion #region 快速驗(yàn)證一個(gè)字符串是否符合指定的正則表達(dá)式。 /// <summary> /// 快速驗(yàn)證一個(gè)字符串是否符合指定的正則表達(dá)式。 /// </summary> /// <param name="_express">正則表達(dá)式的內(nèi)容。</param> /// <param name="_value">需驗(yàn)證的字符串。</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 #region 根據(jù)配置對(duì)指定字符串進(jìn)行 MD5 加密 /// <summary> /// 根據(jù)配置對(duì)指定字符串進(jìn)行 MD5 加密 /// </summary> /// <param name="s"></param> /// <returns></returns> public static string GetMD5(string s) { //md5加密 s = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(s, "md5").ToString(); return s.ToLower().Substring(8, 16); } #endregion #region 得到字符串長(zhǎng)度,一個(gè)漢字長(zhǎng)度為2 /// <summary> /// 得到字符串長(zhǎng)度,一個(gè)漢字長(zhǎng)度為2 /// </summary> /// <param name="inputString">參數(shù)字符串</param> /// <returns></returns> public static int StrLength(string inputString) { System.Text.ASCIIEncoding ascii = new System.Text.ASCIIEncoding(); int tempLen = 0; byte[] s = ascii.GetBytes(inputString); for (int i = 0; i < s.Length; i++) { if ((int)s[i] == 63) tempLen += 2; else tempLen += 1; } return tempLen; } #endregion #region 截取指定長(zhǎng)度字符串 /// <summary> /// 截取指定長(zhǎng)度字符串 /// </summary> /// <param name="inputString">要處理的字符串</param> /// <param name="len">指定長(zhǎng)度</param> /// <returns>返回處理后的字符串</returns> public static string ClipString(string inputString, int len) { bool isShowFix = false; if (len % 2 == 1) { isShowFix = true; len--; } System.Text.ASCIIEncoding ascii = new System.Text.ASCIIEncoding(); int tempLen = 0; string tempString = ""; byte[] s = ascii.GetBytes(inputString); for (int i = 0; i < s.Length; i++) { if ((int)s[i] == 63) tempLen += 2; else tempLen += 1; try { tempString += inputString.Substring(i, 1); } catch { break; } if (tempLen > len) break; } byte[] mybyte = System.Text.Encoding.Default.GetBytes(inputString); if (isShowFix && mybyte.Length > len) tempString += "…"; return tempString; } #endregion #region HTML轉(zhuǎn)行成TEXT /// <summary> /// HTML轉(zhuǎn)行成TEXT /// </summary> /// <param name="strHtml"></param> /// <returns></returns> public static string HtmlToTxt(string strHtml) { string[] aryReg ={ @"<script[^>]*?>.*?</script>", @"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""'])(\\[""'tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>", @"([\r\n])[\s]+", @"&(quot|#34);", @"&(amp|#38);", @"&(lt|#60);", @"&(gt|#62);", @"&(nbsp|#160);", @"&(iexcl|#161);", @"&(cent|#162);", @"&(pound|#163);", @"&(copy|#169);", @"&#(\d+);", @"-->", @"<!--.*\n" }; string newReg = aryReg[0]; string strOutput = strHtml; for (int i = 0; i < aryReg.Length; i++) { Regex regex = new Regex(aryReg[i], RegexOptions.IgnoreCase); strOutput = regex.Replace(strOutput, string.Empty); } strOutput.Replace("<", ""); strOutput.Replace(">", ""); strOutput.Replace("\r\n", ""); return strOutput; } #endregion #region 判斷對(duì)象是否為空 /// <summary> /// 判斷對(duì)象是否為空,為空返回true /// </summary> /// <typeparam name="T">要驗(yàn)證的對(duì)象的類型</typeparam> /// <param name="data">要驗(yàn)證的對(duì)象</param> public static bool IsNullOrEmpty<T>(T data) { //如果為null if (data == null) { return true; } //如果為"" if (data.GetType() == typeof(String)) { if (string.IsNullOrEmpty(data.ToString().Trim())) { return true; } } //如果為DBNull if (data.GetType() == typeof(DBNull)) { return true; } //不為空 return false; } /// <summary> /// 判斷對(duì)象是否為空,為空返回true /// </summary> /// <param name="data">要驗(yàn)證的對(duì)象</param> public static bool IsNullOrEmpty(object data) { //如果為null if (data == null) { return true; } //如果為"" if (data.GetType() == typeof(String)) { if (string.IsNullOrEmpty(data.ToString().Trim())) { return true; } } //如果為DBNull if (data.GetType() == typeof(DBNull)) { return true; } //不為空 return false; } #endregion } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。