【问题标题】:c# How can i replace backslash in a string? [duplicate]c#如何替换字符串中的反斜杠? [复制]
【发布时间】:2018-02-12 16:01:01
【问题描述】:

我正在做一个 c# 项目并且有一些像这样的字符串

第一个字符串

"[\"2018\\/02\\/12\",[\"Test1\",\"Test2\",\"Test3\",\"Test4\"]]"

但是这种字符串格式不适合我的应用。我想把第一个字符串改成这个:

第二个字符串

2018-02-12,"Test1","Test2","Test3","Test4"

我已经完成了一些,但我无法获得反斜杠。实际上反斜杠没有改变。

我的代码:

string MyString = "[\"2018\\/02\\/12\",[\"Test1\",\"Test2\",\"Test3\",\"Test4\"]]";
MyString = MyString.Replace("[", "").Replace("]", "").Replace("\\", "");

我怎样才能得到第二个字符串?

【问题讨论】:

  • 您确定您看到的是 actual 字符串,而不仅仅是调试器显示的转义字符串?
  • 你为什么不看json解析器呢?
  • 您可以将反斜杠与@"" 字符串文字一起使用。喜欢:@"C:\Windows"
  • @P.Knowledge 谢谢,但不工作
  • 尝试使用以下代码:MyString.Replace("[", string.Empty).Replace("]", string.Empty).Replace("\\", string.Empty).Replace (@"\", string.Empty).Replace("/", "-");

标签: c# arrays winforms string-formatting


【解决方案1】:

使用以下代码:

MyString.Replace("[", string.Empty).Replace("]", string.Empty).Replace("\\", string.Empty).Replace(@"\", string.Empty).Replace("/", "-");

然后在 Text Visualizer 中查看结果。

【讨论】:

    猜你喜欢
    • 2011-08-01
    • 2019-05-18
    • 2018-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多