NET-String操作的通用类

本文介绍了一系列实用的C#字符串处理方法,包括字符串分割、拼接、格式转换及去除特定字符等操作。通过具体示例代码展示了如何高效地进行字符串处理。
代码
  1    public class StringPlus
  2ExpandedBlockStart.gifContractedBlock.gif    {
  3        public static List<string> GetStrArray(string str, char speater,bool toLower)
  4ExpandedSubBlockStart.gifContractedSubBlock.gif        {
  5            List<string> list = new List<string>();
  6            string[] ss =  str.Split(speater);
  7            foreach (string s in ss)
  8ExpandedSubBlockStart.gifContractedSubBlock.gif            {
  9                if (!string.IsNullOrEmpty(s) &&!=speater.ToString())
 10ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 11                    string strVal = s;
 12                    if (toLower)
 13ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
 14                        strVal = s.ToLower();
 15                    }

 16                    list.Add(strVal);
 17                }

 18            }

 19            return list;
 20        }

 21        public static string[] GetStrArray(string str)
 22ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 23            return str.Split(new char[',']);
 24        }

 25        public static string GetArrayStr(List<string> list,string speater)
 26ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 27            StringBuilder sb = new StringBuilder();
 28            for (int i = 0; i < list.Count; i++)
 29ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 30                if (i == list.Count - 1)
 31ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 32                    sb.Append(list[i]);
 33                }

 34                else
 35ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 36                    sb.Append(list[i]);
 37                    sb.Append(speater);
 38                }

 39            }

 40            return sb.ToString();
 41        }

 42        
 43        
 44ContractedSubBlock.gifExpandedSubBlockStart.gif        删除最后一个字符之后的字符#region 删除最后一个字符之后的字符
 45
 46ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 47        /// 删除最后结尾的一个逗号
 48        /// </summary>

 49        public static string DelLastComma(string str)
 50ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 51            return str.Substring(0, str.LastIndexOf(","));
 52        }

 53
 54ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 55        /// 删除最后结尾的指定字符后的字符
 56        /// </summary>

 57        public static string DelLastChar(string str,string strchar)
 58ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 59            return str.Substring(0, str.LastIndexOf(strchar));
 60        }

 61
 62        #endregion

 63
 64
 65
 66
 67ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 68        /// 转全角的函数(SBC case)
 69        /// </summary>
 70        /// <param name="input"></param>
 71        /// <returns></returns>

 72        public static string ToSBC(string input)
 73ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 74            //半角转全角:
 75            char[] c = input.ToCharArray();
 76            for (int i = 0; i < c.Length; i++)
 77ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 78                if (c[i] == 32)
 79ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 80                    c[i] = (char)12288;
 81                    continue;
 82                }

 83                if (c[i] < 127)
 84                    c[i] = (char)(c[i] + 65248);
 85            }

 86            return new string(c);
 87        }
        
 88
 89ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 90        ///  转半角的函数(SBC case)
 91        /// </summary>
 92        /// <param name="input">输入</param>
 93        /// <returns></returns>

 94        public static string ToDBC(string input)
 95ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 96            char[] c = input.ToCharArray();
 97            for (int i = 0; i < c.Length; i++)
 98ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 99                if (c[i] == 12288)
100ExpandedSubBlockStart.gifContractedSubBlock.gif                {
101                    c[i] = (char)32;
102                    continue;
103                }

104                if (c[i] > 65280 && c[i] < 65375)
105                    c[i] = (char)(c[i] - 65248);
106            }

107            return new string(c);
108        }

109
110        public static List<string> GetSubStringList(string o_str, char sepeater)
111ExpandedSubBlockStart.gifContractedSubBlock.gif        {
112            List<string> list = new List<string>();
113            string[] ss = o_str.Split(sepeater);
114            foreach (string s in ss)
115ExpandedSubBlockStart.gifContractedSubBlock.gif            {
116                if (!string.IsNullOrEmpty(s) && s != sepeater.ToString())
117ExpandedSubBlockStart.gifContractedSubBlock.gif                {
118                    list.Add(s);
119                }

120            }

121            return list;
122        }

123
124
125ContractedSubBlock.gifExpandedSubBlockStart.gif        将字符串样式转换为纯字符串#region 将字符串样式转换为纯字符串
126        public static string GetCleanStyle(string StrList, string SplitString)
127ExpandedSubBlockStart.gifContractedSubBlock.gif        {
128            string RetrunValue = "";
129            //如果为空,返回空值
130            if (StrList == null)
131ExpandedSubBlockStart.gifContractedSubBlock.gif            {
132                RetrunValue = "";
133            }

134            else
135ExpandedSubBlockStart.gifContractedSubBlock.gif            {
136                //返回去掉分隔符
137                string NewString = "";
138                NewString = StrList.Replace(SplitString, "");
139                RetrunValue = NewString;
140            }

141            return RetrunValue;
142        }

143        #endregion

144
145ContractedSubBlock.gifExpandedSubBlockStart.gif        将字符串转换为新样式#region 将字符串转换为新样式
146        public static string GetNewStyle(string StrList, string NewStyle, string SplitString, out string Error)
147ExpandedSubBlockStart.gifContractedSubBlock.gif        {
148            string ReturnValue = "";
149            //如果输入空值,返回空,并给出错误提示
150            if (StrList == null)
151ExpandedSubBlockStart.gifContractedSubBlock.gif            {
152                ReturnValue = "";
153                Error = "请输入需要划分格式的字符串";
154            }

155            else
156ExpandedSubBlockStart.gifContractedSubBlock.gif            {
157                //检查传入的字符串长度和样式是否匹配,如果不匹配,则说明使用错误。给出错误信息并返回空值
158                int strListLength = StrList.Length;
159                int NewStyleLength = GetCleanStyle(NewStyle, SplitString).Length;
160                if (strListLength != NewStyleLength)
161ExpandedSubBlockStart.gifContractedSubBlock.gif                {
162                    ReturnValue = "";
163                    Error = "样式格式的长度与输入的字符长度不符,请重新输入";
164                }

165                else
166ExpandedSubBlockStart.gifContractedSubBlock.gif                {
167                    //检查新样式中分隔符的位置
168                    string Lengstr = "";
169                    for (int i = 0; i < NewStyle.Length; i++)
170ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
171                        if (NewStyle.Substring(i, 1== SplitString)
172ExpandedSubBlockStart.gifContractedSubBlock.gif                        {
173                            Lengstr = Lengstr + "," + i;
174                        }

175                    }

176                    if (Lengstr != "")
177ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
178                        Lengstr = Lengstr.Substring(1);
179                    }

180                    //将分隔符放在新样式中的位置
181                    string[] str = Lengstr.Split(',');
182                    foreach (string bb in str)
183ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
184                        StrList = StrList.Insert(int.Parse(bb), SplitString);
185                    }

186                    //给出最后的结果
187                    ReturnValue = StrList;
188                    //因为是正常的输出,没有错误
189                    Error = "";
190                }

191            }

192            return ReturnValue;
193        }

194        #endregion

195    }

196

转载于:https://www.cnblogs.com/homezzm/archive/2009/11/27/1612073.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值