以下为代码示例:
/**
* 三种对字符串反向输出的操作方法
* @author mengfeiyang
*
*/
public class WordReverse {
String testWords = "Word Reverse Test Class";
//使用jdk中StringBuffer的reverse方法
public void Test1(){
StringBuffer sb = new StringBuffer();
sb.append(testWords);
System.out.println(sb.reverse());
}
//使用jdk的split方法
public void Test2(){
String[] str = testWords.split(" ");
Stack
stack = new Stack
();
StringBuilder sb = new StringBuilder();
for(int i=0;i
-1;j--){
sb.append(stack.get(j)+" ");
}
System.out.println(sb.toString());
}
//使用char字节自己拼装
public void Test3(){
char[] chars = testWords.toCharArray();
LinkedList
linkedList = new LinkedList
(); Stack
stack = new Stack
(); StringBuffer sb = new StringBuffer(); linkedList.add(0); for(int i = 0;i
-1;x--){ sb.append(stack.get(x)+" "); } System.out.println(sb.toString()); } public String getStr(String str,int startIndex,int endIndex){ return str.substring(startIndex, endIndex); } public static void main(String[] args) { WordReverse wr = new WordReverse(); wr.Test1(); wr.Test2(); wr.Test3(); } } //三个方法的输出结果: ssalC tseT esreveR droW Class Test Reverse Word Class Test Reverse Word