字符串中的首字母大写

编程入门 行业动态 更新时间:2024-10-24 11:15:57
本文介绍了字符串中的首字母大写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在使用字符串将第一个字母转换为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();

更多推荐

字符串中的首字母大写

本文发布于:2023-10-16 05:54:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1496686.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   首字母

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!