ksh*_*tij 6 java string stringtokenizer
例如:
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
Run Code Online (Sandbox Code Playgroud)
现在我不确定如何使用字符串标记符的输出作为另一个的输入?
我也可以使用split方法生成我尝试过的数组
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?
Run Code Online (Sandbox Code Playgroud)
Vit*_*aly 13
使用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());
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11726 次 |
| 最近记录: |