Dimitrios Sria Dimitrios Sria - 2 months ago 13
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("[<>]", "");
temp.trim();

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

System.out.println(content);

Answer

Must be some around this:

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