/** * 字符串指定的部分進行反轉 * @author zhonglinsen * 2016-3-1 下午 */ public class StringReverse { public static void main(String[] args) { String str="mmabcdefnn"; int start=2; int end=7; System.out.println("原先字符串: "+str); String strNew=strReverse(str, start, end); System.out.println("旋轉了指定位置後的字符串: "+strNew); } public static String strReverse(String strArg,int start,int end){ String strStart=strArg.substring(0,start); //System.out.println(strStart); String strEnd=strArg.substring(end+1,strArg.length()); //System.out.println(strEnd); String strSub=strArg.substring(start, end+1); //System.out.println(strSub); StringBuffer sb=new StringBuffer(strSub); strSub=sb.reverse().toString(); //System.out.println(strSub); return strStart.concat(strSub).concat(strEnd); } }
URL:http://www.bianceng.cn/Programming/Java/201608/50365.htm