Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Split string when an uppercase letter is found

Tags:

java

string

regex

I think this is an easy question, but I am not able to find a simple solution (say, less than 10 lines of code :)

I have a String such as "thisIsMyString" and I need to convert it to a String[] {"this", "Is", "My", "String"}.

Please notice the first letter is not uppercase.

like image 719
Guido Avatar asked Sep 20 '10 14:09

Guido


4 Answers

You may use a regexp with zero-width positive lookahead - it finds uppercase letters but doesn't include them into delimiter:

String s = "thisIsMyString";
String[] r = s.split("(?=\\p{Upper})");

Y(?=X) matches Y followed by X, but doesn't include X into match. So (?=\\p{Upper}) matches an empty sequence followed by a uppercase letter, and split uses it as a delimiter.

See javadoc for more info on Java regexp syntax.

EDIT: By the way, it doesn't work with thisIsMyÜberString. For non-ASCII uppercase letters you need a Unicode uppercase character class instead of POSIX one:

String[] r = s.split("(?=\\p{Lu})");
like image 133
axtavt Avatar answered Oct 18 '22 19:10

axtavt


String[] camelCaseWords = s.split("(?=[A-Z])");
like image 27
Bozho Avatar answered Oct 18 '22 17:10

Bozho


For anyone that wonders how the Pattern is when the String to split might start with an upper case character:

String s = "ThisIsMyString";
String[] r = s.split("(?<=.)(?=\\p{Lu})");
System.out.println(Arrays.toString(r));

gives: [This, Is, My, String]

like image 18
Mulder Avatar answered Oct 18 '22 17:10

Mulder


Since String::split takes a regular expression you can use a look-ahead:

String[] x = "thisIsMyString".split("(?=[A-Z])");
like image 8
RoToRa Avatar answered Oct 18 '22 17:10

RoToRa