English 中文(简体)
贾瓦:当发现上案信时,便铺设
原标题:Java: Split string when an uppercase letter is found

我认为这是一个容易的问题,但我无法找到一个简单的解决办法(例如,不到10条法典:)

我有<代码>String,例如> thisIsMyString>,我需要将其改为String[]{>this”、“Is”、“My”、“String>

请注意第一封不是上封信。

最佳回答

您可使用一只带有零宽度正面的 look头的reg,但发现上层字母,但并未将其列入限定范围:

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

<代码>Y(?=X)对匹配sY, 之后是X,但n t中是否包括X。 ∗∗∗∗∗∗∗∗∗ 将其用作限制。

See javadoc for more info on Java regexp syntax.

http://www.un.org。 这样,它就与<编码>有关。 对于非ASCII上层信函,你需要一个统法协会的上层品级,而不是PPOSIX:

String[] r = s.split("(?=\p{Lu})");
问题回答
String[] camelCaseWords = s.split("(?=[A-Z])");

任何人,如果怀疑这种模式是怎样的,试图分立可能会从高案开始:

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

说明: 页: 1

String:split 定期表达你们可以使用一个头目:

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

;;

static Pattern p = Pattern.compile("(?=\p{Lu})");
String[] s1 = p.split("thisIsMyFirstString");
String[] s2 = p.split("thisIsMySecondString");

...

该校将分在Caps上,先拨。 因此,它应当处理 came事和适当的案件。

(?<=.)(?=(\p{Upper}))

TestText = Test, Text
thisIsATest = this, Is, A, Test

一项简单的 s/java建议,如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.
}

测试:

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

它生产:

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

还将reg调成数字。

      String str = "IAmAJavaProgrammer";
  StringBuilder expected = new StringBuilder(); 
for (int i = 0; i < str.length(); i++) {

        if(Character.isUpperCase(str.charAt(i))){    
            expected.append(" ");
        }
        expected.append(str.charAt(i));


}

System.out.println(expected);




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签