字符串中的首字母大写 [英] Capitalise first letter in String
本文介绍了字符串中的首字母大写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在使用字符串将第一个字母转换为Capital时遇到问题:
I'm having trouble converting the first letter to Capital in a String:
rackingSystem.toLowerCase(); // has capitals in every word, so first convert all to lower case
StringBuilder rackingSystemSb = new StringBuilder();
rackingSystemSb.append(rackingSystem);
rackingSystemSb.setCharAt(0, Character.toUpperCase(rackingSystemSb.charAt(0)));
rackingSystem = rackingSystemSb.toString();
这似乎不起作用..
有任何建议吗?
推荐答案
尝试做:
rackingSystem = rackingSystem.toLowerCase();
而不是:
rackingSystem.toLowerCase();
字符串是不可变的,你必须重新分配的结果toLowerCase()
。
Strings are immutable, you must reassign the result of toLowerCase()
.
虽然更容易,(只要您的字符串大于长度2):
Easier though, (as long as your String is larger than length 2):
rackingSystem = rackingSystem.substring(0,1).toUpperCase() + rackingSystem.substring(1).toLowerCase();
这篇关于字符串中的首字母大写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文