views:

158

answers:

3

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.

+11  A: 

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 too. For non-ASCII uppercase letters you need a Unicode uppercase character class instead of POSIX one:

String[] r = s.split("(?=\\p{Lu})");
axtavt
+1, you were first (by a few seconds), + explanation, + the predefined upper class :)
Bozho
very good solution (+1)
seanizer
Just be aware that if the upper character is the last in the string that you won't receive an empty str for the bit beyond it, e.g. splitting "H2W" will return { "", "2" } where "" is the bit before "H", and "2" is the bit in the middle. Normally not an issue but it can break assumptions about how many parts are returned.
locka
+2  A: 
String[] camelCaseWords = s.split("(?=[A-Z])");
Bozho
Doesn't work for "thisIsMyÜberString".
jarnbjo
@jarnbjo - and what does? And perhaps downvote a few other answers that don't?
Bozho
axtavt already gave a correct answer to Guido's question, so why answer twice? Your answer is wrong, so I downvoted it and pointed out why.
jarnbjo
@jarnbjo his answer initially did not work for umlaut.
Bozho
+2  A: 

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

String[] x = "thisIsMyString".split("(?=[A-Z])");
RoToRa