Java: Split string when an uppercase letter is found

前端 未结 7 975
醉梦人生
醉梦人生 2020-11-27 15:29

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 \"thisIs

7条回答
  •  春和景丽
    2020-11-27 15:55

    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.

提交回复
热议问题