>> Is there a way to split a string by line breaks ?
>> lines = new String[10];
>> lines = data.split ('\n');
You have just allocated an array of ten Strings, then immediately lost
your reference to it. Try:
String[] lines = s.split("\n");
>> his didnt work
>
> Fixed it it had to be "\\n" instead of '\n'
Only a single backslash should be needed, i.e. "\n".
import java.io.PrintWriter;
public class Main {
public static void main(String[] args) {
PrintWriter out = new PrintWriter(System.out, true);
String s = "hello\nworld";
String[] lines = s.split("\n");
int lineNo = 0;
for(String line: lines) {
out.println(++lineNo + ": " + line);
}
}
}
C:\files\usenet\java>java -cp . Main
1: hello
2: world