Replace empty line

1

I have a variable with the following content:

10
20 30
40 50 60

70 80 90


100

There are spaces (may have 2 or more followed) and line breaks (also several in the sequence), and I want to substitute to stay this way:

10
20
30
40
50
60
70
80
90
100

Each value in a row, with no empty rows and no spaces.

I have tried conteudo = conteudo.replace(" ", "\n"); but empty lines remain.

Can you replace everything at once or clean these empty lines after breaking all values?

    
asked by anonymous 26.04.2018 / 20:48

1 answer

4

To remove empty rows you can use the replaceAll method with the regular expression \n+

String tratar = "10\n"
        + "20 30\n"
        + "40 50 60\n"
        + "\n"
        + "70 80 90\n"
        + "\n"
        + "\n"
        + "100";

String nova = tratar.replace(" ", "\n").replaceAll("\n+", "\n");

System.out.println(nova);

Functional example: link

    
26.04.2018 / 20:59