Java: Split string when an uppercase letter is found

Viewed 61762

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.

7 Answers

A simple scala/java suggestion that does not split at entire uppercase strings like NYC:

def splitAtMiddleUppercase(token: String): Iterator[String] = {
   val regex = """[\p{Lu}]*[^\p{Lu}]*""".r
   regex.findAllIn(token).filter(_ != "") // did not find a way not to produce empty strings in the regex. Open to suggestions.
}

test with:

val examples = List("catch22", "iPhone", "eReplacement", "TotalRecall", "NYC", "JGHSD87", "interÜber")
for( example <- examples) {
   println(example + " -> "  + splitAtMiddleUppercase(example).mkString("[", ", ", "]"))
}

it produces:

    catch22 -> [catch22]
    iPhone -> [i, Phone]
    eReplacement -> [e, Replacement]
    TotalRecall -> [Total, Recall]
    NYC -> [NYC]
    JGHSD87 -> [JGHSD87]
    interÜber -> [inter, Über]

Modify the regex to cut at digits too.

Related