Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to split a java text block by lines?

Let’s say we have the following java text block :

         String textblock = """
                 A : 111
                 B : 111
                 C : 1111
                 """;

I would like to split it by lines to get the following result :

         String [] splitedTextBlock = splitTextBlockSomeHow(textblock); 
         // expected result : {"A : 111", "B : 222", "C : 333"}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

public static String[] splitTextBlock(String textblock) {
   return textblock.trim().split(System.lineSeparator());
}

...

String textblock = """
        A : 111
        B : 111
        C : 1111
        """;
        
String[] splitedTextBlock = splitTextBlock(textblock);
Arrays.stream(splitedTextBlock).forEach(System.out::println);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading