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<length;i++){ sbStr.append(chars[length-i-1]+"");//用StringBuffer将其逆转. } return sbStr.toString();//转换为String } public static String commonsEncode(String bstr) { return new String(new org.apache.commons.codec.binary.Base64() .encode(bstr.getBytes())); } public static String replaceChar(String str){ //替换单引号 if(str.indexOf("%27")!=-1){ str=str.replace("%27", "'"); } //替换双引号 if(str.indexOf("%28")!=-1){ str=str.replace("%28", """); } return str; } }