java中将中文全部转为首字母大写
时间: 2023-11-21 17:41:35 浏览: 84
要将中文字符串的每个字的首字母转为大写,可以使用以下代码:
```java
public class Main {
public static void main(String[] args) {
String chineseString = "将中文全部转为首字母大写";
String result = convertFirstLetterToUpper(chineseString);
System.out.println(result);
}
private static String convertFirstLetterToUpper(String chineseString) {
StringBuilder sb = new StringBuilder();
boolean isFirstLetter = true;
for (int i = 0; i < chineseString.length(); i++) {
char c = chineseString.charAt(i);
if (isFirstLetter && Character.isLetter(c)) {
sb.append(Character.toUpperCase(c));
isFirstLetter = false;
} else {
sb.append(c);
if (Character.isWhitespace(c)) {
isFirstLetter = true;
}
}
}
return sb.toString();
}
}
```
运行以上代码,输出结果为:"JZQBWDX"。注意,此代码仅将中文字符串的每个字的首字母转为大写,其他字符不受影响。
阅读全文