Split Java String by New Line

前端 未结 20 1110
予麋鹿
予麋鹿 2020-11-22 00:56

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|

相关标签:
20条回答
  • 2020-11-22 01:39

    In JDK11 the String class has a lines() method:

    Returning a stream of lines extracted from this string, separated by line terminators.

    Further, the documentation goes on to say:

    A line terminator is one of the following: a line feed character "\n" (U+000A), a carriage return character "\r" (U+000D), or a carriage return followed immediately by a line feed "\r\n" (U+000D U+000A). A line is either a sequence of zero or more characters followed by a line terminator, or it is a sequence of one or more characters followed by the end of the string. A line does not include the line terminator.

    With this one can simply do:

    Stream<String> stream = str.lines();
    

    then if you want an array:

    String[] array = str.lines().toArray(String[]::new);
    

    Given this method returns a Stream it upon up a lot of options for you as it enables one to write concise and declarative expression of possibly-parallel operations.

    0 讨论(0)
  • 2020-11-22 01:39

    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 :

    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.

    0 讨论(0)
  • 2020-11-22 01:43
    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]);
            }
        }
    
    }
    
    0 讨论(0)
  • 2020-11-22 01:49

    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.

    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
    • it does not match multiple new lines
    • it does not remove trailing empty lines
    0 讨论(0)
  • 2020-11-22 01:50

    For preserving empty lines from getting squashed use:

    String lines[] = String.split("\\r?\\n", -1);
    
    0 讨论(0)
  • 2020-11-22 01:51

    The above answers did not help me on Android, thanks to the Pshemo response that worked for me on Android. I will leave some of Pshemo's answer here :

    split("\\\\n")
    
    0 讨论(0)
提交回复
热议问题