feige 通过本文主要向大家介绍了c#定义字符串数组,c#中定义一个字符串,c#定义字符串,c#定义一个字符串数组,c#定义一个字符串等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
本文实例讲述了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.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 str