字符串倒叙、英语单词整体倒叙
字符串取反
String string = "abc123"; // 方式一 StringBuffer sBuffer1 = new StringBuffer(string); String string1 = sBuffer1.reverse().toString(); System.out.println(string1); // 方式二 char[] cs = string.toCharArray(); StringBuffer sBuffer2 = new StringBuffer(); for (int i = cs.length - 1; i >= 0; i--) { sBuffer2.append(cs[i]); } String string2 = sBuffer2.toString(); System.out.println(string2);
英语句子单词整体取反
String string = "i #love you&sunny"; String[] strings = string.split("\\s|#|&"); List<String> words = Arrays.asList(strings); Collections.reverse(words); StringBuffer sBuffer = new StringBuffer(); for (String string2 : words) { sBuffer.append(string2 + " "); } String reWords = sBuffer.toString();