@Test
public void translateString2Sequence() {
String regex = "[A-Z]{1}[a-z]{1,}";
String str = "privatePhilipsReconstructionStorage";
Matcher matcher = Pattern.compile(regex).matcher(str);
int position = 0;
StringBuilder temp = new StringBuilder();
while (matcher.find()) {
int start = matcher.start();
int end = matcher.end();
String content = matcher.group(0);
if (position < start) {
String sub = str.substring(position, start);
temp.append(sub).append(" ");
}
temp.append(content).append(" ");
position = end;
}
if (position < str.length()) {
temp.append(str.substring(position));
}
StringBuilder sequence = new StringBuilder().append(Character.toUpperCase(temp.charAt(0))).append(temp.substring(1));
System.out.println(sequence.toString().trim());
}
输出:Private Philips Reconstruction Storage
正则表达式Matcher用法参考:java中使用正则表达式_老朱.的博客-CSDN博客_java 正则表达式



