不用Split方法实现分割字符串
今天在网上看见有人问这个问题,我动手写了下。哈哈 这只是第一步实现,没考虑优化,也没想有没有什么隐藏的bug,不过初步运行是没什么问题了。分享一下吧。
List<string> strL=new List<string>(); string temp = ""; string str = "12,13,14.15"; foreach (char c in str) { if (',' == c || '.' == c) { strL.Add(temp); temp = ""; } else { temp = temp + c; } } strL.Add(temp); string[] strA = new string[strL.Count]; strL.CopyTo(strA);
最后把strA返回就行了,返回了一个分割好的字符数组。