要执行反向切换以拆分字符串中的单词,请使用 split() 方法反转每个单词,将每个单词的首字母更改为小写,其余字母更改为大写。
import java.lang.StringBuffer; public class ToggleReverse { public static void main(String args[]){ String sample = "Hello How are you"; String[] words = sample.split(" "); String result = ""; for(String word:words){ StringBuffer s = new StringBuffer(word); word = s.reverse().toString(); String firstSub = word.substring(0, 1); String secondSub = word.substring(1); result = result+firstSub.toLowerCase()+secondSub.toUpperCase()+" "; } System.out.println(result); } }
输出结果
oLLEH wOH eRA uOY