如何使用Java将字符串中的第一个字母大写? [英] How to capitalize the first letter of word in a string using Java?

查看:75
本文介绍了如何使用Java将字符串中的第一个字母大写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例字符串

one thousand only
two hundred
twenty
seven

如何更改大写字母中字符串的第一个字符,而不更改任何其他字符串的大小写字母?

How do I change the first character of a string in capital letter and not change the case of any of the other letters?

更改后应该是:

One thousand only
Two hundred
Twenty
Seven

注意:我不喜欢我不想使用apache.commons.lang.WordUtils来做这件事。

Note: I don't want to use the apache.commons.lang.WordUtils to do this.

推荐答案

如果你只想把第一个大写名为输入的字符串的字母,其余部分保留:

If you only want to capitalize the first letter of a string named input and leave the rest alone:

String output = input.substring(0, 1).toUpperCase() + input.substring(1);

现在输出将有你想要的。在使用之前检查你的输入至少是一个字符,否则你会得到一个例外。

Now output will have what you want. Check that your input is at least one character long before using this, otherwise you'll get an exception.

这篇关于如何使用Java将字符串中的第一个字母大写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆