例如:
String s =“这是a.line是.over”
应该出来
“这是a.Line is.Over”
我想过两次使用字符串标记器
-first split using"." -second split using " " to get the first word -then change charAt[0].toUpper
String a="this is.a good boy"; String [] dot=a.split("\\."); while(i<dot.length) { String [] sp=dot[i].split(" "); sp[0].charAt(0).toUpperCase();// what to do with this part?
解决方法
使用StringBuilder,无需拆分和创建其他字符串,依此类推,请参阅代码
public static void main(String... args) { String text = "this is a.line is. over"; int pos = 0; boolean capitalize = true; StringBuilder sb = new StringBuilder(text); while (pos < sb.length()) { if (sb.charAt(pos) == '.') { capitalize = true; } else if (capitalize && !Character.isWhitespace(sb.charAt(pos))) { sb.setCharAt(pos,Character.toUpperCase(sb.charAt(pos))); capitalize = false; } pos++; } System.out.println(sb.toString()); }