Laurens Weyn
Laurens Weyn

Reputation: 84

converting a String into a String array in Java

I simply cannot find how to do this. so, I have a String that will be something like this:

do this
then do that
then more of this

and I need to turn this into an array of strings. where every new line is needs to be a separate entry in the array. I need this done so I can process every line or command separately. I can't enter it directly into an array because this is loaded from a text document, and there is some code that removes comments and unnecessary empty lines, and a few more things with the string before it needs to become an array.

thanks in advance!

Upvotes: 0

Views: 148

Answers (2)

assylias
assylias

Reputation: 328913

String s = "do this\nthen do that\nthen more of this";
String[] split = s.split("\n");

Upvotes: 2

adarshr
adarshr

Reputation: 62613

Try this:

string.split("(?m)\n");

Upvotes: 1

Related Questions