【搬家】【.NET】【C#】C# 中处理路径中多余的反斜线

来源:互联网 发布:java分布式爬虫框架 编辑:程序博客网 时间:2024/06/05 17:47

本文最早于 2013年10月2日于本人个人博客(http://mooowooo.tk)发表,现博客搬家至此,转载请注明出处。

在编程中涉及到路径操作时,我们时常犯一些低级的错误,诸如拼写错误,单词结尾是否有s等,但还有一个经常出错的细节便是输入了多余的反斜线\。对于拼写问题,暂时没有什么精妙的解决方案,只能依靠人脑的记忆,但是对于多余的反斜线,我们可以通过在程序里内置处理函数来达到解决问题的目的。一下是本人最近复习 C# 过程中,写的3个处理字符串中冗余反斜线的代码,个人感觉还是比较全面了,能够判断诸如开头的反斜线,盘符不合法等问题,虽然都是用的很傻的逻辑,但至少功能上是没有问题的,如果在阅读本文的朋友里有人有更好的想法,欢迎通过任何渠道告知我,我也会及时将其实现并更新到本文里。

String类方法版:

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace net{    classProgram    {        staticvoid Main(string[] args)        {            string path = "";       // 输入的路径            string newpath = "";    // 处理后的路径            path = Console.ReadLine();            char separator = '\\';  // 分割字符            string[] splitstrings = new string[100];    // 储存分割后每段的数组            splitstrings = path.Split(separator);            //  处理部分            for (int count = 0; count < splitstrings.Length; count++)            {                if (splitstrings[count] != "")                {                    newpath += splitstrings[count] + '\\';                }            }            newpath += "file.txt";  // 将处理后的路径与文件名连接,成为文件的完整路径            Console.WriteLine(newpath);            Console.ReadLine();        }    }}

StringBuilder版:

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace net{    classProgram    {        staticvoid Main(string[] args)        {            string path = "";       // 输入的路径            string newpath = "";    // 处理后的路径            path = Console.ReadLine();            char separator = '\\';  // 分割字符            string[] splitstrings = new string[100];    // 储存分割后每段的数组            splitstrings = path.Split(separator);            //  处理部分            for (int count = 0; count < splitstrings.Length; count++)            {                if (splitstrings[count] != "")                {                    newpath += splitstrings[count] + '\\';                }            }            newpath += "file.txt";  // 将处理后的路径与文件名连接,成为文件的完整路径            Console.WriteLine(newpath);            Console.ReadLine();        }    }}

纯StringBuilder版:

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace net{    classProgram    {        staticvoid Main(string[] args)        {            string path = "";       // 输入的路径            StringBuilder newpath = new StringBuilder(256);    // 处理后的路径            path = Console.ReadLine();            char separator = '\\';  // 分割字符            string[] splitstrings = new string[100];    // 储存分割后每段的数组            splitstrings = path.Split(separator);            //  处理部分            for (int count = 0; count < splitstrings.Length; count++)            {                if (splitstrings[count] != "")                {                    newpath.Append(splitstrings[count] + '\\');                }            }            newpath。Append("file.txt");  // 将处理后的路径与文件名连接,成为文件的完整路径            Console.WriteLine(newpath);            Console.ReadLine();            }        }}


0 0
原创粉丝点击