我想大写每个单词的第一个字母。当我输入test test test
它改变为Test Test Test
,但是如果我在test test test
之间使用很多空格,它不会将首字母改为大写。我应该如何更改我的代码才能正常工作?在单词之间使用大量空格时,第一个字母不会变成大写字母
public class Solution
{
public static void main(String[] args) throws IOException
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
try{
String test = reader.readLine();
String[] testSplit = test.split(" ");
StringBuilder sb = new StringBuilder();
for (int I =0 ; I < testSplit.length ; I ++){
String capitalVer = capitalizeFirstLetter(testSplit[I]);
if(I != 0){
sb.append(" ");
}
sb.append(capitalVer);
}
System.out.println(sb.toString());
}
catch(Exception es)
{
}
}
public static String capitalizeFirstLetter(final String string)
{
if (string == null || string.equals(""))
throw new NullPointerException("no string ");
return Character.toUpperCase(string.charAt(0)) + string.substring(1);
}
}
您的代码,如图所示,不会产生你所描述的(不需要)输出。 –