处理字符串中的""以进行文件路径处理"\"



我有一个函数,我想将字符串值C:samplec#programsConverter转换为C:\samplec#programs\Converter 请注意差异。这是我的函数:

private string FilePathProcessor(string path)
{
    char[] oriCharArray = path.ToCharArray; 
    List<char> oriCharList = new List<char>;
    List<char> newCharList = new List<char>;
    foreach (char item in oriCharArray)
    {
        oriCharList.Add(item);
    }
    foreach (char items in oriCharList)
    {
        if ((items != "\"))
        {
            newCharList.Add(items);
        }
        else
        {
            newCharList.Add(items);
            newCharList.Add(items);
        }
    }
    string result = string.Join(",", newCharList.ToArray());
    return result;
}

当然,此功能可以满足我的需求。但是,我想知道 .Net 中是否已经有一个现有的函数可以处理它。我只是在清理我的代码并检查更简单,更快的解决方案。如果已经有办法,就不会重新发明轮子。

使用 String.Replace((

string path = @"C:samplec#programsConverter";
string output = path.Replace("\", @"\");

最新更新