30

我无法将字符串中的第一个字母转换为大写字母:

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();

这个好像不行。。

有什么建议么?

4

2 回答 2

67

尝试做:

rackingSystem = rackingSystem.toLowerCase();

代替:

rackingSystem.toLowerCase(); 

字符串是不可变的,您必须重新分配toLowerCase().

不过更简单(只要您的字符串大于长度 2):

rackingSystem = rackingSystem.substring(0,1).toUpperCase() + rackingSystem.substring(1).toLowerCase();
于 2013-03-06T23:02:06.570 回答
18
StringBuilder rackingSystemSb = new StringBuilder(rackingSystem.toLowerCase());
rackingSystemSb.setCharAt(0, Character.toUpperCase(rackingSystemSb.charAt(0)));
rackingSystem = rackingSystemSb.toString();
于 2013-03-06T23:05:47.110 回答