代码之家  ›  专栏  ›  技术社区  ›  Antônio Medeiros

用-n替换新的行字符

  •  3
  • Antônio Medeiros  · 技术社区  · 6 年前

    我正在编写一个ide。假设我有 Hello, World 字符串上的pascal程序:

    String sourceCode = "program Hello;\nbegin\nwriteln ('Hello, world.');\nend.";
    

    如果我这样做 System.out.println(sourceCode); 输出显示:

    program Hello;
    begin
    writeln ('Hello, world.');
    end.
    

    太好了,但是我想把新行显示为 \n . 我试过:

    sourceCode = sourceCode.replaceAll(System.lineSeparator(), "\\n");
    

    但是然后 system.out.println(源代码); 输出:

    program Hello;nbeginnwriteln ('Hello, world.');nend.
    

    如我所料 \n 显示:

    program Hello;\nbegin\nwriteln ('Hello, world.');\nend.
    

    我怎样才能做到这一点?完整演示:

    public class PrintPascalHelloWorld {
        public static void main(String[] args) {
            String sourceCode = "program Hello;\nbegin\nwriteln ('Hello, world.');\nend.";
            System.out.println(sourceCode);
            sourceCode = sourceCode.replaceAll(System.lineSeparator(), "\\n");
            System.out.println(sourceCode);
        }
    }
    

    我可以使用 Online Java IDE .

    4 回复  |  直到 6 年前
        1
  •  3
  •   khelwood Muhammed Elsayed.radwan    6 年前

    你不需要使用 replaceAll 除非你想用正则表达式。 replace 工作良好。

    sourceCode = sourceCode.replace("\n", "\\n");
    
        2
  •  4
  •   davidxxx    6 年前

    你用 String.replaceAll(String regex, String replacement) 第一个参数是 String regex 作为第二个 String replacement .
    这种方法对于替换有一些特殊性 String . 尤其是,如果它包含 \ 或A $ ,它不能解释为文字。

    根据 Matcher.replaceAll() 指定用于发动机罩下 String.replaceAll() :

    注意,替换项中的反斜杠()和美元符号($) 字符串可能导致结果不同于 作为文字替换字符串处理。美元符号可能会被处理 作为上述捕获子序列的引用,以及 反斜杠用于转义替换中的文字字符 一串 .

    对你来说, sourceCode.replaceAll(System.lineSeparator(), "\\n") 将脱离文字 n 性格。

    如果你想用 replaceAll() ,应使用复杂的方式,例如:

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

    因为不需要使用正则表达式,所以使用 String.replace(CharSequence target, CharSequence replacement) 取代了 CharSequence 由另一个和 字符序列 .
    因此您可以调用:

    sourceCode = sourceCode.replace("\n", "\\n")
    

    现在哪里 字符串替换 作为文字处理。


    还要注意 System.lineSeparator() 当您在文件outputstream中写入新行字符不可避免地依赖于os时,这是有意义的。
    这个 \n 包含在 "program Hello;\nbegin\nwriteln ('Hello, world.');\nend."; 不依赖于操作系统。

    例如,在windows上,jvm 8对windows新行字符的处理是无差别的。( \r\n ) \n 在标准输出中打印时。

    Windows上的此代码:

    String sourceCode = "program Hello;\r\nbegin\nwriteln ('Hello, world.');\r\nend.";
    System.out.println(sourceCode);
    

    确实产生:

    程序你好;

    开始

    writeln(“你好,世界。”);

    结束。

        3
  •  3
  •   Karol Dowbecki    6 年前

    自从 System.lineSeparator() 是否依赖于平台,根据文档:

    在UNIX系统上,返回“\n”;在Microsoft Windows系统上,返回“\r\n”。

    用起来会更好 \n 直接:

    String sourceCode = "program Hello;\nbegin\nwriteln ('Hello, world.');\nend.";
    System.out.println(sourceCode);
    sourceCode = sourceCode.replaceAll("\n", "\\\\n");
    System.out.println(sourceCode);
    

    将显示:

    program Hello;
    begin
    writeln ('Hello, world.');
    end.
    program Hello;\nbegin\nwriteln ('Hello, world.');\nend.
    
        4
  •  0
  •   Harshal Khachane    6 年前

    如果你在windows机器上测试代码,那么它肯定会失败。AS

    System.lineSeparator()
    

    将在Windows上返回“\r\n”。它与输入字符串中的“\n”不匹配。

    您需要更改为下面的代码

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