我在将字母串中的第一个字母转换为Capital时遇到了麻烦:
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();
这似乎不起作用..
有什么建议吗?
答案 0 :(得分:67)
尝试做:
rackingSystem = rackingSystem.toLowerCase();
而不是:
rackingSystem.toLowerCase();
字符串是不可变的,您必须重新分配toLowerCase()
的结果。
虽然更容易,(只要你的String大于长度2):
rackingSystem = rackingSystem.substring(0,1).toUpperCase() + rackingSystem.substring(1).toLowerCase();
答案 1 :(得分:18)
StringBuilder rackingSystemSb = new StringBuilder(rackingSystem.toLowerCase());
rackingSystemSb.setCharAt(0, Character.toUpperCase(rackingSystemSb.charAt(0)));
rackingSystem = rackingSystemSb.toString();