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!

share|improve this question

60% accept rate
feedback

2 Answers

up vote 2 down vote accepted
String s = "do this\nthen do that\nthen more of this";
String[] split = s.split("\n");
share|improve this answer
thanks for the quick reply! I really didn't know it was that easy! – Laurens Weyn Apr 3 '12 at 9:10
@LaurensWeyn Consider accepting one answer if it solved your issue. – assylias Apr 3 '12 at 11:05
feedback

Try this:

string.split("(?m)\n");
share|improve this answer
Just curious, how does it differ split("\n")? (I'm always looking to learn new regex tricks :) – Leigh Apr 4 '12 at 1:21
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.