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

Use the java.lang.String split function with a limit.

String foo = "some string with spaces";
String parts[] = foo.split(" ", 2);
System.out.println(String.format("cr: %s, cdr: %s", parts[0], parts[1]));

You will get:

cr: some, cdr: string with spaces

Must be some around this:

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

string.split(" ",2)

split takes a limit input restricting the number of times the pattern is applied.

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String,%20int)

Tags:

Java

String