简体   繁体   English

Java String split()丢失字符串

[英]Java String split() loses string

I have a String called raw . 我有一个名为raw的字符串。 I am trying to split it into an array like so: 我正在尝试将其拆分为一个数组,如下所示:

lines = raw.split("\\\\r?\\\\n|\\\\r");

This works well for the first few occurrences but then it breaks and totally loses the rest of the string. 这对于前几次出现效果很好,但随后会中断并完全丢失字符串的其余部分。 Eg raw is This is my string\\n\\nThis is a new paragraph\\nThis is another line and becomes {"This is my string", "", "This is a new paragraph"} . 例如raw是This is my string\\n\\nThis is a new paragraph\\nThis is another line并变为{"This is my string", "", "This is a new paragraph"} Is this a bug within Java or am I doing something wrong? 这是Java中的错误,还是我做错了什么? How can I fix it? 我该如何解决?

Edit: I do want to keep blank lines. 编辑:我想保持空白行。 [\\\\n\\\\r]+ does not keep blank lines [\\\\n\\\\r]+不保留空白行

我会使用正则表达式:

raw.split("[\\r\\n]+");

Your code works as expected: 您的代码按预期工作:

class Test {
    public static void main(String[] args) {
        String raw = "This is my string\n\nThis is a new paragraph\nThis is another line";
        String[] lines = raw.split("\\r?\\n|\\r");
        for (String line : lines) {
            System.out.println(line);
        }
    }
}

This prints: 打印:

This is my string

This is a new paragraph
This is another line

It is therefore likely that the problem is with how you examine/display the result of split() , not with the split() itself. 因此,问题可能出在您如何检查/显示split()的结果,而不是split()本身。

You could clean up that regex liek this: 您可以清理该正则表达式:

[\\n\\r]+

the + means it will look for whitespace as far as it can before splitting +表示它将在分割之前尽可能地寻找空格

chances are there's a big in how you're trying to view the answer or something else, I could help you more if you show some code. 您尝试查看答案或其他方式的机会很大,如果您显示一些代码,我可以为您提供更多帮助。

if you want to keep the spaces , try 如果要保留空间 ,请尝试

(?=[\\n\\r]+)

You could use the "multi line" flag and simply split on end-of-line: 您可以使用“多行”标志,并在行尾简单地分割:

lines = raw.split("(?m)$\\s*");

The term \\s* consumes the newline characters. 术语\\s*使用换行符。


Here's some test code: 这是一些测试代码:

String raw  = "This is my string\n\nThis is a new paragraph\nThis is another line";
String[] lines = raw.split("(?m)$\\s*");
System.out.println(Arrays.toString( lines));

Output: 输出:

[This is my string, This is a new paragraph, This is another line]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM