【问题标题】:c# replace from "\\", to "\" characterc# 将 "\\" 替换为 "\" 字符
【发布时间】:2021-04-02 12:09:34
【问题描述】:

我需要从这个字符转换;

"\\\\192.168.2.11\\MKRPRTAL-2.11\\TechnicalDocuments\\TechnicalDocumentsCore\\1000020200-test5_ED_R1.pdf"

到:

"192.168.2.11\\MKRPRTAL-2.11\TechnicalDocuments\TechnicalDocumentsCore\1000020200-test5_ED_R1.pdf"

我试过了

rawPath = response2.TechDocSeriBilgiGetirList[0].PhysicalPathCore.Replace(@"\\", @"\").Replace(@"/", @"\");

(response2.TechDocSeriBilgiGetirList[0].PhysicalPathCore) 等于;

"\\\\192.168.2.11\\MKRPRTAL-2.11\\TechnicalDocuments\\TechnicalDocumentsCore\\1000020200-test5_ED_R1.pdf"

【问题讨论】:

标签: c# replace backslash


【解决方案1】:

字符串操作是不可变的。因此,要检查操作后需要检查 rawPath 的值,而不是执行这些操作的字符串,因为该值不会改变。

查看此链接:

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/strings/#:~:text=String%20objects%20are%20immutable%3A%20they,in%20a%20new%20string%20object.

【讨论】:

    【解决方案2】:

    您应该考虑到\ 字符用\\ 表示 因为反斜杠是特殊字符。 例如\n 表示换行符。就像您按下了键盘上的 Enter 键一样。

    尝试运行这段代码:

    System.Console.WriteLine("I am in first line\nAnd i am in the next line");
    

    输出将是:

    I am in first line
    And i am in the next line
    

    同样的事情也适用于\\。 试试这个:

    System.Console.WriteLine("\\");
    

    它只会输出一个反斜杠! (\)

    我的猜测是您正在使用 Visual Studio 调试器查看变量的值。尝试使用System.Console.WriteLine 方法打印它们。 它将显示变量的“真实值”。

    您还可以附加您发布的那行代码的结果吗?

    【讨论】:

      【解决方案3】:

      请尝试以下代码:

      string strvar = @"\\\\192.168.2.11\\MKRPRTAL-2.11\\TechnicalDocuments\\TechnicalDocumentsCore\\1000020200-test5_ED_R1.pdf";        
      string strrep = strvar.Replace(@"\\\\", @"").Replace(@"\\", @"\");
      

      输出将是:

      192.168.2.11\MKRPRTAL-2.11\TechnicalDocuments\TechnicalDocumentsCore\1000020200-test5_ED_R1.pdf
      

      【讨论】:

      • 我试过了,但是没用。我的值是:“\\\\192.168.2.11\\MKRPRTAL-2.11/TechnicalDocuments/TechnicalDocumentsCore/1000020200-test5_ED_R1.pdf” 但我需要“\\192.168.2.11\MKRPRTAL-2.11\TechnicalDocuments\TechnicalDocumentsCore\1000020200-test5_ED_R1 .pdf"
      猜你喜欢
      • 1970-01-01
      • 2011-08-27
      • 2020-11-17
      • 1970-01-01
      • 2010-11-16
      • 2011-06-08
      • 1970-01-01
      • 2011-11-20
      • 2018-09-15
      相关资源
      最近更新 更多