【问题标题】:Escape escape characters in spring boot [duplicate]在春季启动中转义转义字符[重复]
【发布时间】:2017-10-24 01:50:44
【问题描述】:

给定一个字符串,我想用一个斜杠替换所有双斜杠,然后用- 替换所有\n 实例。例如,如果我有这个:

\\n\\nff

应该转换成:

\n\nff

然后到:

--ff

我写了这段代码:

shippingAddress = shippingAddress.replaceAll("\\\\n", "\n");

例如:

public static void main(String[] args) {
    System.out.println("xx");
    String x = "enterchar17\\n\\n\\nenterchar17enterchar17\\n\\n\\n\\nenterchar17";
    System.out.println(x);
    x.replaceAll("\\n","\n");
    System.out.println(x);
}

这会产生这个输出:

 xx
enterchar17\n\n\nenterchar17enterchar17\n\n\n\nenterchar17
enterchar17\n\n\nenterchar17enterchar17\n\n\n\nenterchar17

所以它没有改变。我需要逃避转义字符,因为弹簧靴以某种方式发送了 2 个斜线。我能做什么?

我认为 JSON 确实认为 double 是 double。它不会让它逃脱吗?

"line1": "fasfsafasfsa\n\n\nff\n\tasas",

在邮递员上,当我这样发送请求时,\n 被转换为“-”

但在邮递员中,当我使用双精度发送时,它无法转换。

当我检查发送字符串请求的页面时,我看到了这个

line1
:
"enterchar17↵↵v↵↵enterchar17↵↵↵enterchar17"

在铬上

【问题讨论】:

    标签: java json spring


    【解决方案1】:

    我认为你有 java 问题。线

    x.replaceAll("\\n","\n");
    

    应该是

    x = x.replaceAll("\\n","\n");
    

    String 是一个不可变的类。因此,方法的输出需要在变量中捕获。

    【讨论】:

      猜你喜欢
      • 2018-08-29
      • 2017-12-05
      • 2023-03-26
      • 2019-03-17
      • 1970-01-01
      • 1970-01-01
      • 2019-06-01
      • 2013-12-08
      • 1970-01-01
      相关资源
      最近更新 更多