如何将 String 或 CharSequence 的一部分转换为大写?

how to convert part of a String or CharSequence to uppercase?

有没有办法将字符串(或 CharSequence)的一部分转换为大写?

理想情况下,一种产生以下行为的方法。

String hello = "hello";
System.out.println(hello.toUpperCase(0,3);
//Output: HEL

我知道 toUpperCase() 但我只想将字符串的一部分大写。

使用substring()获取字符串的一部分然后改变它的大小写

 String hello = "hello";
 String temp  = hello.subString(0,3).toUpperCase();
 temp += hello.subString(3);
 System.out.println(temp);  //  HELlo

您可以通过两次调用来完成此操作 - substring to get the sequence you're interested in and then toUpperCase 将其大写:

System.out.println(hello.substring(0, 3).toUpperCase());