字符串示例
one thousand only
two hundred
twenty
seven
我如何改变一个大写字母的字符串的第一个字符,而不改变任何其他字母的情况?
更改之后应该是:
One thousand only
Two hundred
Twenty
Seven
注意:我不想使用apache.commons.lang.WordUtils来做这件事。
字符串示例
one thousand only
two hundred
twenty
seven
我如何改变一个大写字母的字符串的第一个字符,而不改变任何其他字母的情况?
更改之后应该是:
One thousand only
Two hundred
Twenty
Seven
注意:我不想使用apache.commons.lang.WordUtils来做这件事。
当前回答
用StringBuilder解决方案:
value = new StringBuilder()
.append(value.substring(0, 1).toUpperCase())
.append(value.substring(1))
.toString();
. .基于之前的答案
其他回答
另外,Spring Framework中还有org.springframework.util.StringUtils:
StringUtils.capitalize(str);
如果你只想大写一个名为input的字符串的第一个字母,其余的保持不变:
String output = input.substring(0, 1).toUpperCase() + input.substring(1);
现在输出将有您想要的内容。在使用此方法之前,请检查您的输入至少是一个字符,否则将会出现异常。
StringUtils.capitalize(str)
来自apache commons-lang。
这很简单,只需要一行代码。 if String A = scanner.nextLine(); 然后您需要这样写以显示首字母大写的字符串。
System.out.println(A.substring(0, 1).toUpperCase() + A.substring(1));
现在已经完成了。
使用StringTokenizer类的示例:
String st = " hello all students";
String st1;
char f;
String fs="";
StringTokenizer a= new StringTokenizer(st);
while(a.hasMoreTokens()){
st1=a.nextToken();
f=Character.toUpperCase(st1.charAt(0));
fs+=f+ st1.substring(1);
System.out.println(fs);
}