Splitting a String into array using newline characters ("\n")

We can split a String & store it into an array using split() method. But, when it comes to a newline character , we need to use a regex for this .

The code will be:

String splittedArray[] = myString.split("\\r?\\n");

If you need to remove the blank lines & not store them in the array, you may use this:

String splittedArray[] = myString.split("[\\r?\\n]+");


Happy Learning

No comments :

Post a Comment