【问题标题】:Replace line break with escape character用转义字符替换换行符
【发布时间】:2016-04-22 18:26:30
【问题描述】:

我有一个应用程序,我从 Excel 中读取数据并将其写入 txt。我正在从 excel 中读取单元格,这可能带有换行符。例如,我可以阅读:

你好

你好吗?

如下图所示

我必须将它保存在一行中,并带有相应的转义字符。它必须是这样的。

但是是这样保存的:

我错过了什么?

【问题讨论】:

  • 我错过了什么? - 代码,一方面。

标签: java excel escaping


【解决方案1】:

找到答案here

String repl = str.replaceAll("(\\r|\\n|\\r\\n)+", "\\\\n")

【讨论】:

    【解决方案2】:

    您需要在保存之前转义您的\

    String escaped = string.replaceAll("\n", "\\n");
    

    【讨论】:

    • 这不起作用,因为结果是“你好,你好吗?”
    【解决方案3】:

    您需要转义字符“\”,因此将您的文本视为:

    "Hello \\n how are you?"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多