字符串示例
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来做这件事。
当前回答
public String capitalizeFirstLetter(String original) {
if (original == null || original.length() == 0) {
return original;
}
return original.substring(0, 1).toUpperCase() + original.substring(1);
}
只是…一个完整的解决方案,我看到它只是结合了其他人最终发布的内容=P。
其他回答
实际上,在这种情况下,如果避免使用+运算符并使用concat(),您将获得最佳性能。它是合并两个字符串的最佳选择(但不太适合合并很多字符串)。在这种情况下,代码看起来是这样的:
String output = input.substring(0, 1).toUpperCase().concat(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));
现在已经完成了。
给定输入字符串:
Character.toUpperCase(input.charAt(0)) + input.substring(1).toLowerCase()
我的函数方法。它在整个段落的白转义后将句子中的第一个字符颠倒。
如果只启用单词的第一个字符,只需删除.split(" ")
b.name.split(" ")
.filter { !it.isEmpty() }
.map { it.substring(0, 1).toUpperCase()
+it.substring(1).toLowerCase() }
.joinToString(" ")