简体   繁体   中英

How to split a String my Empty lines in java

can anyone help me how to split string by empty lines? for example i have following string

abc def ghk
122 saf fsaf

sfa fasf

afsf

i want to split above string by empty lines and output should be as follows

string[0] = "abc def ghk
    122 saf fsaf"
String[1]="sfa fasf"
String[2]="afsf"

Thanks in advance

You can split the string on a newline that has only whitespace until the next newline:

String input = "abc def ghk\n122 saf fsaf\n\nsfa fasf afsf";
String[] split = input.split("\n\\s*\n");
System.out.println(split[0]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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