在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
今天编写小程序时,被字符串中出现的空格卡住了,”一怒之下“就搜罗了网络上的多种解决字符串中的空格的方法。呵呵,分享一下... 实际上没有八种方法的,只是一个喜欢的标题,呵呵,你懂得.... 源文件http://pan.baidu.com/share/link?shareid=426222&uk=3912660076 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 去除空格 { class Program { static void Main(string[] args) { string BugsBug = " my name is Bugs Bug "; string test1BugsBug = " this is Bugs Bug test 1 "; string test2BugsBug = " this is Bugs Bug test 2 "; string test3BugsBug = " this is Bugs Bug test 3 "; string test4BugsBug = " this is Bugs Bug test 4 "; string test5BugsBug = " this is Bugs Bug test 5 "; string test6BugsBug = " this is Bugs Bug test 6 "; string test7BugsBug = " this is Bugs Bug test 7 "; string test8BugsBug = " this is Bugs Bug test 8 "; string test9BugsBug = " this is Bugs Bug test 9 "; //1. Console.WriteLine("Original: " + "'" + BugsBug + "'"); Console.WriteLine("Trim(): " + "'" + BugsBug.Trim() + "'"); Console.WriteLine(); //2. Console.WriteLine("Original: " + "'" + test1BugsBug + "'"); Console.WriteLine("TrimStart(): " + "'" + test1BugsBug.TrimStart() + "'"); Console.WriteLine(); //3. Console.WriteLine("Original: " + "'" + test2BugsBug + "'"); Console.WriteLine("TrimEnd(): " + "'" + test2BugsBug.TrimEnd() + "'"); Console.WriteLine(); //4. //参考源:http://blog.csdn.net/dyufei/article/details/5420407 感谢原作者 //ASCII表:http://www.asciima.com/ Console.WriteLine("Original: " + "'" + test3BugsBug + "'"); //在~里面获取每一个characters CharEnumerator ce = test3BugsBug.GetEnumerator(); string test3BugsBugcopy = string.Empty; while (ce.MoveNext()) { byte[] array = new byte[1]; //此方法返回byte[]; array = System.Text.ASCIIEncoding.ASCII.GetBytes(ce.Current.ToString()); if (array[0] != 32) test3BugsBugcopy += ce.Current.ToString(); } Console.WriteLine("ASCII: " + "'" + test3BugsBugcopy + "'"); Console.WriteLine(); //5. Console.WriteLine("Original: " + "'" + test4BugsBug + "'"); string test4BugsBugcopy = string.Empty; if (test4BugsBug.IndexOf(" ") > -1)//预防非法替换 test4BugsBugcopy = test4BugsBug.Replace(" ", string.Empty); Console.WriteLine("Replace(): " + "'" + test4BugsBugcopy + "'"); Console.WriteLine(); //6. Console.WriteLine("Original: " + "'" + test5BugsBug + "'"); string test5BugsBugcopy = string.Empty; foreach (char item in test5BugsBug.ToCharArray()) { //可加参数arg... if (item!=Convert.ToChar(" ")) { test5BugsBugcopy += item; } } Console.WriteLine("ToCharArray():" + "'" + test5BugsBugcopy + "'"); Console.WriteLine(); //7. //Split()参考:http://msdn.microsoft.com/en-us/library/y7h14879.aspx string test6BugsBugcopy=string.Empty; Console.WriteLine("Original: " + "'" + test6BugsBug + "'");//以" "分割,并且不保留" "元素. string[] array1 = test6BugsBug.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); test6BugsBugcopy = string.Join(string.Empty, array1);//拼接...也可以用" "拼接,相当于只保留一个空格. Console.WriteLine("Split&&Join():" + "'" + test6BugsBugcopy + "'"); Console.WriteLine(); //8. //全角半角参考:http://www.cnblogs.com/dudu/archive/2011/09/17/2179423.html Console.WriteLine("Original: " + "'" + test7BugsBug + "'"); test7BugsBug = System.Text.RegularExpressions.Regex.Replace(test7BugsBug, @"\s*", string.Empty); Console.WriteLine("Regex: " + "'" + test7BugsBug + "'"); Console.WriteLine(); //9. //Append()参考:http://msdn.microsoft.com/zh-cn/library/ds86a594.aspx Console.WriteLine("Original: " + "'" + test8BugsBug + "'"); string[] array3 = test8BugsBug.Split(' ');//char..拆分 StringBuilder sb = new StringBuilder(); foreach (var item in array3) { if (item.Trim() != string.Empty) { sb.Append(item);//组合... //sb.Append(" ");以一个空格为分割 } } Console.WriteLine("Substring: " + "'" + sb + "'"); Console.WriteLine(); //9. Console.ReadKey(); } } } 运行结果 |
请发表评论