0

Okay, so I have searched throughout google and this site and have not found a solution that works how I want for this problem. I have a string, for example "javaRegexConfusion" where I want to split by capital letters, while also maintaining them. The problem here is, if I were to split directly by capital letters, one way would leave me without the letters at all, and another I know of would split them into its own object on the array. The result in the string above I want is an array consisting of "java", "Regex", and "Confusion". I am completely unsure of how to go about it. I am sure the solution is extremely simple, however I do not know how to phrase what I am looking for, hence the confusing title.

EDIT: Found the answer in a totally off topic quest. I am not really familiar with the regexs accepted, and would never have figured out

While I like AlexWien's answer, I prefer this method. Instead of iterating through and then splitting wherever capitals were, the writer of the other answer split right before anything capital.

String[] output = input.split("(?=(?!^)[A-Z])");

I really like this method and will most likely use this from now on (too bad that I will never remember "(?=(?!^)[A-Z])") without looking at it)

4

1 回答 1

0

只需逐个字符地遍历字符串,如果找到一个字符,isUpperCase()则复制到结果字符串数组。存储当前位置,然后继续..

利用

Character.isUpperCase(s.charAt(pos))
于 2013-01-13T01:49:57.783 回答