Java: Split string when an uppercase letter is found
Solution 1:
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})");
Solution 2:
String[] camelCaseWords = s.split("(?=[A-Z])");
Solution 3:
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]
Solution 4:
Since String::split
takes a regular expression you can use a look-ahead:
String[] x = "thisIsMyString".split("(?=[A-Z])");
Solution 5:
This regex will split on Caps, omitting the first. So it should work for camel-case and proper-case.
(?<=.)(?=(\\p{Upper}))
TestText = Test, Text
thisIsATest = this, Is, A, Test