Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在与平台无关的字符串中查找所有换行符或特征线字符并将其替换为\n_Java_Regex_String - Fatal编程技术网

Java 在与平台无关的字符串中查找所有换行符或特征线字符并将其替换为\n

Java 在与平台无关的字符串中查找所有换行符或特征线字符并将其替换为\n,java,regex,string,Java,Regex,String,我正在寻找一种合适而健壮的方法,从字符串中查找并替换所有换行符或特征线字符,这些字符与\n的任何操作系统平台无关 这是我尝试过的,但效果并不好 public static String replaceNewLineChar(String str) { try { if (!str.isEmpty()) { return str.replaceAll("\n\r", "\\n") .replaceAll("\n

我正在寻找一种合适而健壮的方法,从
字符串
中查找并替换所有
换行符
特征线
字符,这些字符与
\n
的任何操作系统平台无关

这是我尝试过的,但效果并不好

public static String replaceNewLineChar(String str) {
    try {
        if (!str.isEmpty()) {
            return str.replaceAll("\n\r", "\\n")
                    .replaceAll("\n", "\\n")
                    .replaceAll(System.lineSeparator(), "\\n");
        }
        return str;
    } catch (Exception e) {
        // Log this exception
        return str;
    }
}
例如:

输入字符串:

This is a String
and all newline chars 
should be replaced in this example.
预期输出字符串:

This is a String\nand all newline chars\nshould be replaced in this example.
但是,它返回了相同的输入字符串。喜欢它放置\n并再次将其解释为换行符。
请注意,如果您想知道为什么有人希望
\n
在其中,这是用户的一项特殊要求,需要将字符串放在XML后缀中。

如果您希望使用文字
\n
,则以下内容应该可以使用:

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

这似乎很有效:

String s = "This is a String\nand all newline chars\nshould be replaced in this example.";
System.out.println(s);
System.out.println(s.replaceAll("[\\n\\r]+", "\\\\n"));

顺便说一句,您不需要捕捉异常。

哦,当然,您可以用一行正则表达式来完成,但这有什么乐趣呢

public static String fixToNewline(String orig){
    char[] chars = orig.toCharArray();
    StringBuilder sb = new StringBuilder(100);
    for(char c : chars){
        switch(c){
            case '\r':
            case '\f':
                break;
            case '\n':
                sb.append("\\n");
                break;
            default:
                sb.append(c);
        }
    }
    return sb.toString();
}

public static void main(String[] args){
   String s = "This is \r\n a String with \n Different Newlines \f and other things.";

   System.out.println(s);
   System.out.println();
   System.out.println("Now calling fixToNewline....");
   System.out.println(fixToNewline(s));

}
结果

This is 
 a String with 
 Different Newlines  and other things.

Now calling fixToNewline....
This is \n a String with \n Different Newlines  and other things.

为什么它不好用?您编写了预期的输出字符串,为什么不把实际的输出放进去?发现我的错误,它应该是ne
\\\\n
,奇怪的谢谢,仍然不明白为什么\\n不起作用。不幸的是,我处于完全的C#模式。谢谢,发现我的错误,请查看我对问题的评论。只需使用替换方法:String repl=str.replace(“\r\n”,“\\n”);你确定晚了一分钟吗?如果您看到时间戳,我的答案在您接受答案前5分钟发布:)@ChP:非常感谢您遵守so:P:-)的不成文协议,请您解释一下,为什么\\n不起作用,但是\\\\n。它与C#配合得很好。那么Java有什么不同呢?实际上在Java中,
String的第二个参数repalcell
也需要双转义作为第一个参数,因为可以使用
$1
$2
等。因此,
`输入为
\\`您好,我已经做了,以替换换行符。你能把这个表达式贴在我得到原始字符串的地方吗。