In java, the specified part of the string is reversed, and in java, the string is reversed.
This blog post adopts java implementation: rotate the specified part of the string, for example, str = "mmabcdfeffnn". If you specify abcdfeff to rotate the part, str = "mmffefdcbann" is obtained ".
The implementation idea is as follows: the source string is divided into three parts: the header, to be rotated, and the tail. All three parts need to be intercepted, for the part to be rotated, a technique is used here, that is, StringBuffer (String string) is used as the intermediary and the reverse () method is called to rotate. The last three parts are as follows: Header, rotated, and tail. Connect to concat (String string.
The Code is as follows:
/*** Reverse the specified part of the String * @ author zhonglinsen * 2016-3-1 pm */public class StringReverse {public static void main (String [] args) {String str = "mmabcdefnn"; int start = 2; int end = 7; System. out. println ("original String:" + str); String strNew = strReverse (str, start, end); System. out. println ("the String after the specified position is rotated:" + strNew);} public static String strReverse (String strArg, int start, int end) {String strStart = strArg. substring (0, start); String strEnd = strArg. substring (end + 1, strArg. length (); String strSub = strArg. substring (start, end + 1); StringBuffer sb = new StringBuffer (strSub); strSub = sb. reverse (). toString (); return strStart. concat (strSub ). concat (strEnd );}}