我想大寫每個單詞的第一個字母。當我輸入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);
}
}
您的代碼,如圖所示,不會產生你所描述的(不需要)輸出。 –