I'm trying to get the first paragraph of a text (until a line break) on a variable, and the rest thing in another variable.
For example for a text like:
hello how are you im really fine thank you, and you, im fine. thanks a
lot good bye.
foo bar random text foo bar random text foo bar random
text foo bar random text foo bar random text .
blahblahblah
blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah.
String firstpart = "hello how are you im really fine thank you, and you, im fine. thanks a lot good bye."
String restofit = "foo bar random text foo bar random text foo bar random text foo bar random text foo bar random text \n lahblahblah blahblahblah blahblahblah blahblahblah blahblahblah blahblahblah."
String[] result = mytext.split("\n", 2);
System.out.println(result[0]);
if (result[1] != null) System.out.println("nas");
java.lang.ArrayIndexOutOfBoundsException: 1
result[1]
You need to escape the backslash:
String[] result = mytext.split("\\R",2);
//check length of result:
if(result.length >1){
//do what you need with result[1];
}
Java turns a double backslash \\
in a string into one backslash in runtime. This is because of special characters such as "
,'
.