Dimitrios Sria Dimitrios Sria - 7 months ago 35
Java Question

How to split a string from the first space occurence only Java

I tried to split a string using string.index and string.length but i get an error that string is out of range. How can i fix that?

while (in.hasNextLine()) {

String temp = in.nextLine().replaceAll("[<>]", "");

String nickname = temp.substring(temp.indexOf(' '));
String content = temp.substring(' ' + temp.length()-1);



Must be some around this:

String nickname = temp.substring(0, temp.indexOf(' '));
String content = temp.substring(temp.indexOf(' ') + 1);