In java, the specified part of the string is reversed, and in java, the string is reversed.

Source: Internet
Author: User

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 );}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.