Dimitrios Sria Dimitrios Sria - 1 year ago 127
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);


Answer Source

Must be some around this:

String nickname = temp.substring(0, temp.indexOf(' '));
String content = temp.substring(temp.indexOf(' ') + 1);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download