package com.mall.common;public class Method { public static void main(String args[]){ String str1="I am a student!"; System.out.println("原字符串:"); System.out.println(str1); String str2=reverse(str1); System.out.println("逆转后字符串:"); System.out.println(str2); String s=",6,3,1"; System.out.println("原字符串 : "+s); System.out.println("逆转后字符串:"+wordReverse(s)); } //单词逆转 public static String reverse(String str){ String[]strarray=str.split("\\s+"); StringBuffer sbf=new StringBuffer(); for(int i=strarray.length-1;i>=0;i--) sbf.append(strarray[i]+" "); return sbf.toString(); } //字符逆转 public static String wordReverse(String str){ int n=str.length(); char []chars=new char[n]; str.getChars(0, n, chars, 0);//获得了char[]可操作的数组. int length=chars.length; StringBuffer sbStr=new StringBuffer(); for(int i=0;i