非常实用的C#字符串操作处理类StringHelper.cs

时间:2021-05-19

一个非常好的C#字符串操作处理类StringHelper.cs,具体内容如下

/// <summary> /// 类说明:Assistant /// 编 码 人:苏飞 /// 联系方式:361983679 /// 更新网站:http://ma(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.Replace(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.Replace("'", ""); String = String.Replace("\"", ""); return String; } String = String.Replace("'", "'"); String = String.Replace("\"", """); 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 #region 根据配置对指定字符串进行 MD5 加密 /// <summary> /// 根据配置对指定字符串进行 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 得到字符串长度,一个汉字长度为2 /// <summary> /// 得到字符串长度,一个汉字长度为2 /// </summary> /// <param name="inputString">参数字符串</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 截取指定长度字符串 /// <summary> /// 截取指定长度字符串 /// </summary> /// <param name="inputString">要处理的字符串</param> /// <param name="len">指定长度</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转行成TEXT /// <summary> /// HTML转行成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 判断对象是否为空 /// <summary> /// 判断对象是否为空,为空返回true /// </summary> /// <typeparam name="T">要验证的对象的类型</typeparam> /// <param name="data">要验证的对象</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> /// 判断对象是否为空,为空返回true /// </summary> /// <param name="data">要验证的对象</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 } }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章