用新行分隔Java字符串。

时间:2021-11-27 02:15:02

I'm trying to split text in a JTextArea using a regex to split the String by \n However, this does not work and I also tried by \r\n|\r|n and many other combination of regexes. Code:

我尝试在一个JTextArea中使用regex分割文本,但是,这并不奏效,我还尝试了\r\n|\r|n和许多其他的regex组合。代码:

public void insertUpdate(DocumentEvent e) {
    String split[], docStr = null;
    Document textAreaDoc = (Document)e.getDocument();

    try {
        docStr = textAreaDoc.getText(textAreaDoc.getStartPosition().getOffset(), textAreaDoc.getEndPosition().getOffset());
    } catch (BadLocationException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    split = docStr.split("\\n");
}

15 个解决方案

#1


587  

This should cover you:

这应包括:

String lines[] = string.split("\\r?\\n");

There's only really two newlines (UNIX and Windows) that you need to worry about.

您需要担心的只有两个新行(UNIX和Windows)。

#2


105  

If you don’t want empty lines:

如果你不想要空行:

String.split("[\\r\\n]+")

#3


69  

split method is using regex (regular expressions). Since Java 8 regex supports \R which represents (from documentation of Pattern class):

split方法使用regex(正则表达式)。因为Java 8 regex支持\R(来自模式类的文档):

Linebreak matcher
\R         Any Unicode linebreak sequence, is equivalent to \u000D\u000A|[\u000A\u000B\u000C\u000D\u0085\u2028\u2029]

Linebreak matcher \R任何Unicode Linebreak序列,相当于\u000D\u000A|[\u000A\ u000C\u000D\ u2028\ u2028]

So we can use it to match:

所以我们可以用它来匹配

  • \u000D\000A -> \r\n pair
  • \ u000D \ 000 - > \ r \ n
  • \u000A -> line feed (\n)
  • \u000A ->线路馈电(\n)
  • \u000B -> line tabulation (DO NOT confuse with character tabulation \t which is \u0009)
  • \u000B ->行表格(不要混淆了字符表\t是\u0009)
  • \u000C -> form feed (\f)
  • \u000C ->型饲料(\f)
  • \u000D -> carriage return (\r)
  • \u000D ->回车(\r)
  • \u0085 -> next line (NEL)
  • \u0085 ->下一行(NEL)
  • \u2028 -> line separator
  • \ u2028 - >行分隔符
  • \u2029 -> paragraph separator
  • \ u2029 - >段落分隔符

As you see \r\n is placed at start of regex which ensures that regex will try to first match this pair, and only if it fails it will try to match single character line separators.

当您看到\r\n被放置在regex的开始,确保regex将尝试第一次匹配这一对,并且只有当它失败时,它将尝试匹配单个字符行分隔符。


So if you want to split on line separator use split("\\R").

因此,如果您想在分隔线分隔符上使用split(“\\R”)。

If you don't want to remove from resulting array trailing empty strings "" use split(regex, limit) with negative limit parameter like split("\\R", -1).

如果您不想从结果数组中删除空字符串“”使用split(regex, limit),带负极限参数,比如split(“\\R”,-1)。

If you want to treat one or more continues empty lines as single delimiter use split("\\R+").

如果您想要处理一个或多个连续的空行,则作为单个分隔符使用split(“\\R+”)。

#4


39  

String.split(System.getProperty("line.separator"));

This should be system independent

这应该是系统独立的。

#5


11  

You don't have to double escape characters in character groups.

你不需要在字符组中双重转义字符。

For all non empty lines use:

对于所有非空行使用:

String.split("[\r\n]+")

#6


7  

Maybe this would work:

也许这将工作:

Remove the double backslashes from the parameter of the split method:

从分割方法的参数中删除双反斜杠:

split = docStr.split("\n");

#7


4  

For preserving empty lines from getting squashed use:

为了防止空行被压扁:

String lines[] = String.split("\\r?\\n", -1);

#8


4  

All answers given here actually do not respect Javas definition of new lines as given in e.g. BufferedReader#readline. Java is accepting \n, \r and \r\n as new line. Some of the answers match multiple empty lines or malformed files. E..g. <sometext>\n\r\n<someothertext> when using [\r\n]+would result in two lines.

这里给出的所有答案实际上都不尊重Javas定义的新行,例如,BufferedReader#readline。Java接受\n, \r和\r\n作为新行。一些答案匹配多个空行或格式错误的文件。E . g。 \n\r\n 当使用[\r\n]+会导致两行。

String lines[] = string.split("(\r\n|\r|\n)", -1);

In contrast, the answer above has the following properties:

相比之下,上述答案具有以下特性:

  • it complies with Javas definition of a new line such as e.g. the BufferedReader is using it
  • 它符合Javas定义的新行,例如,BufferedReader正在使用它。
  • it does not match multiple new lines
  • 它不匹配多个新行。
  • it does not remove trailing empty lines
  • 它不会删除尾随的空行。

#9


3  

The above code doesnt actually do anything visible - it just calcualtes then dumps the calculation. Is it the code you used, or just an example for this question?

上面的代码实际上并没有做任何可见的事情——它只是calcualtes然后转储计算。是您使用的代码,还是这个问题的示例?

try doing textAreaDoc.insertString(int, String, AttributeSet) at the end?

试着做textAreaDoc。在末端的insertString(int, String, AttributeSet) ?

#10


1  

String lines[] =String.split( System.lineSeparator())

字符串行[]=字符串。split(System.lineSeparator())

#11


1  

As an alternative to the previous answers, guava's Splitter API can be used if other operations are to be applied to the resulting lines, like trimming lines or filtering empty lines :

作为前一个答案的替代方案,如果要将其他操作应用到生成的行中,比如修剪行或过滤空行,则可以使用guava的Splitter API。

import com.google.common.base.Splitter;

Iterable<String> split = Splitter.onPattern("\r?\n").trimResults().omitEmptyStrings().split(docStr);

Note that the result is an Iterable and not an array.

注意,结果是可迭代的,而不是数组。

#12


0  

After failed attempts on the basis of all given solutions. I replace \n with some special word and then split. For me following did the trick:

在所有给定的解决方案的基础上失败的尝试之后。我用一些特殊的词来代替,然后分开。对我来说,下面的窍门是:

article = "Alice phoned\n bob.";
article = article.replace("\\n", " NEWLINE ");
String sen [] = article.split(" NEWLINE ");

I couldn't replicate the example given in the question. But, I guess this logic can be applied.

我无法复制这个问题中给出的例子。但是,我想这个逻辑是可以应用的。

#13


0  

  • try this hope it was helpful for you
  • 试试这个希望对你有帮助。

 String split[], docStr = null;
Document textAreaDoc = (Document)e.getDocument();

try {
    docStr = textAreaDoc.getText(textAreaDoc.getStartPosition().getOffset(), textAreaDoc.getEndPosition().getOffset());
} catch (BadLocationException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}

split = docStr.split("\n");

#14


0  

If, for some reason, you don't want to use String.split (for example, because of regular expressions) and you want to use functional programming on Java 8 or newer:

如果,出于某种原因,您不想使用字符串。split(例如,由于正则表达式),您想要在Java 8或更新上使用函数式编程:

List<String> lines = new BufferedReader(new StringReader(string))
        .lines()
        .collect(Collectors.toList());

#15


-2  

package in.javadomain;

public class JavaSplit {

    public static void main(String[] args) {
        String input = "chennai\nvellore\ncoimbatore\nbangalore\narcot";
        System.out.println("Before split:\n");
        System.out.println(input);

        String[] inputSplitNewLine = input.split("\\n");
        System.out.println("\n After split:\n");
        for(int i=0; i<inputSplitNewLine.length; i++){
            System.out.println(inputSplitNewLine[i]);
        }
    }

}

#1


587  

This should cover you:

这应包括:

String lines[] = string.split("\\r?\\n");

There's only really two newlines (UNIX and Windows) that you need to worry about.

您需要担心的只有两个新行(UNIX和Windows)。

#2


105  

If you don’t want empty lines:

如果你不想要空行:

String.split("[\\r\\n]+")

#3


69  

split method is using regex (regular expressions). Since Java 8 regex supports \R which represents (from documentation of Pattern class):

split方法使用regex(正则表达式)。因为Java 8 regex支持\R(来自模式类的文档):

Linebreak matcher
\R         Any Unicode linebreak sequence, is equivalent to \u000D\u000A|[\u000A\u000B\u000C\u000D\u0085\u2028\u2029]

Linebreak matcher \R任何Unicode Linebreak序列,相当于\u000D\u000A|[\u000A\ u000C\u000D\ u2028\ u2028]

So we can use it to match:

所以我们可以用它来匹配

  • \u000D\000A -> \r\n pair
  • \ u000D \ 000 - > \ r \ n
  • \u000A -> line feed (\n)
  • \u000A ->线路馈电(\n)
  • \u000B -> line tabulation (DO NOT confuse with character tabulation \t which is \u0009)
  • \u000B ->行表格(不要混淆了字符表\t是\u0009)
  • \u000C -> form feed (\f)
  • \u000C ->型饲料(\f)
  • \u000D -> carriage return (\r)
  • \u000D ->回车(\r)
  • \u0085 -> next line (NEL)
  • \u0085 ->下一行(NEL)
  • \u2028 -> line separator
  • \ u2028 - >行分隔符
  • \u2029 -> paragraph separator
  • \ u2029 - >段落分隔符

As you see \r\n is placed at start of regex which ensures that regex will try to first match this pair, and only if it fails it will try to match single character line separators.

当您看到\r\n被放置在regex的开始,确保regex将尝试第一次匹配这一对,并且只有当它失败时,它将尝试匹配单个字符行分隔符。


So if you want to split on line separator use split("\\R").

因此,如果您想在分隔线分隔符上使用split(“\\R”)。

If you don't want to remove from resulting array trailing empty strings "" use split(regex, limit) with negative limit parameter like split("\\R", -1).

如果您不想从结果数组中删除空字符串“”使用split(regex, limit),带负极限参数,比如split(“\\R”,-1)。

If you want to treat one or more continues empty lines as single delimiter use split("\\R+").

如果您想要处理一个或多个连续的空行,则作为单个分隔符使用split(“\\R+”)。

#4


39  

String.split(System.getProperty("line.separator"));

This should be system independent

这应该是系统独立的。

#5


11  

You don't have to double escape characters in character groups.

你不需要在字符组中双重转义字符。

For all non empty lines use:

对于所有非空行使用:

String.split("[\r\n]+")

#6


7  

Maybe this would work:

也许这将工作:

Remove the double backslashes from the parameter of the split method:

从分割方法的参数中删除双反斜杠:

split = docStr.split("\n");

#7


4  

For preserving empty lines from getting squashed use:

为了防止空行被压扁:

String lines[] = String.split("\\r?\\n", -1);

#8


4  

All answers given here actually do not respect Javas definition of new lines as given in e.g. BufferedReader#readline. Java is accepting \n, \r and \r\n as new line. Some of the answers match multiple empty lines or malformed files. E..g. <sometext>\n\r\n<someothertext> when using [\r\n]+would result in two lines.

这里给出的所有答案实际上都不尊重Javas定义的新行,例如,BufferedReader#readline。Java接受\n, \r和\r\n作为新行。一些答案匹配多个空行或格式错误的文件。E . g。 \n\r\n 当使用[\r\n]+会导致两行。

String lines[] = string.split("(\r\n|\r|\n)", -1);

In contrast, the answer above has the following properties:

相比之下,上述答案具有以下特性:

  • it complies with Javas definition of a new line such as e.g. the BufferedReader is using it
  • 它符合Javas定义的新行,例如,BufferedReader正在使用它。
  • it does not match multiple new lines
  • 它不匹配多个新行。
  • it does not remove trailing empty lines
  • 它不会删除尾随的空行。

#9


3  

The above code doesnt actually do anything visible - it just calcualtes then dumps the calculation. Is it the code you used, or just an example for this question?

上面的代码实际上并没有做任何可见的事情——它只是calcualtes然后转储计算。是您使用的代码,还是这个问题的示例?

try doing textAreaDoc.insertString(int, String, AttributeSet) at the end?

试着做textAreaDoc。在末端的insertString(int, String, AttributeSet) ?

#10


1  

String lines[] =String.split( System.lineSeparator())

字符串行[]=字符串。split(System.lineSeparator())

#11


1  

As an alternative to the previous answers, guava's Splitter API can be used if other operations are to be applied to the resulting lines, like trimming lines or filtering empty lines :

作为前一个答案的替代方案,如果要将其他操作应用到生成的行中,比如修剪行或过滤空行,则可以使用guava的Splitter API。

import com.google.common.base.Splitter;

Iterable<String> split = Splitter.onPattern("\r?\n").trimResults().omitEmptyStrings().split(docStr);

Note that the result is an Iterable and not an array.

注意,结果是可迭代的,而不是数组。

#12


0  

After failed attempts on the basis of all given solutions. I replace \n with some special word and then split. For me following did the trick:

在所有给定的解决方案的基础上失败的尝试之后。我用一些特殊的词来代替,然后分开。对我来说,下面的窍门是:

article = "Alice phoned\n bob.";
article = article.replace("\\n", " NEWLINE ");
String sen [] = article.split(" NEWLINE ");

I couldn't replicate the example given in the question. But, I guess this logic can be applied.

我无法复制这个问题中给出的例子。但是,我想这个逻辑是可以应用的。

#13


0  

  • try this hope it was helpful for you
  • 试试这个希望对你有帮助。

 String split[], docStr = null;
Document textAreaDoc = (Document)e.getDocument();

try {
    docStr = textAreaDoc.getText(textAreaDoc.getStartPosition().getOffset(), textAreaDoc.getEndPosition().getOffset());
} catch (BadLocationException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}

split = docStr.split("\n");

#14


0  

If, for some reason, you don't want to use String.split (for example, because of regular expressions) and you want to use functional programming on Java 8 or newer:

如果,出于某种原因,您不想使用字符串。split(例如,由于正则表达式),您想要在Java 8或更新上使用函数式编程:

List<String> lines = new BufferedReader(new StringReader(string))
        .lines()
        .collect(Collectors.toList());

#15


-2  

package in.javadomain;

public class JavaSplit {

    public static void main(String[] args) {
        String input = "chennai\nvellore\ncoimbatore\nbangalore\narcot";
        System.out.println("Before split:\n");
        System.out.println(input);

        String[] inputSplitNewLine = input.split("\\n");
        System.out.println("\n After split:\n");
        for(int i=0; i<inputSplitNewLine.length; i++){
            System.out.println(inputSplitNewLine[i]);
        }
    }

}