编写一个 java 程序在字符串中对每个单词大小写 tOGGLE?
您可以通过使用 toUpperCase() 和 toLowerCase() 方法更改单词字母的大小写。
使用 split()方法分割字符串中的每个单词,将每个单词的第一个字母更改为小写,其余字母更改为大写。
示例
public class Sample{
public static void main(String args[]){
String sample = "Hello How are you";
String[] words = sample.split(" ");
String result = "";
for(String word:words){
String firstSub = word.substring(0, 1);
String secondSub = word.substring(1);
result = result+firstSub.toLowerCase()+secondSub.toUpperCase()+" ";
}
System.out.println(result);
}
}输出
hELLO hOW aRE yOU
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP