Gets a substring with an inverted string of the maximum length

Source: Internet
Author: User

/*******************************************************************************


* @Description string operation class


* @Author CManLH@163.com


* @Date 2008/05/24


   ******************************************************************************/


public class Stringx {


    /*


* @Function obtains the maximum length from the specified string a substring of the inverted string


* @Parm str string source string


* @Return has the largest substring of the inverted string, and when there are multiple, only the rightmost one is returned


     */


public string getmaxlenreversestring (String str) {


int startIndex =-1;


int lenofreversestr = 2;


int tmp =-1;


for (int i = Lenofreversestr i <= str.length (); i++) {


tmp = getposofreversestring (str, i);


if (TMP!=-1) {


lenofreversestr = i;


StartIndex = tmp;


str = str.substring (0, tmp + LENOFREVERSESTR);


} else {


break;


        }


      }


return StartIndex = = 1? Str.substring (0,1): str.substring (Startindex,startindex + lenofreversestr);


    }


    /*


* @Function from the specified string, gets the starting position of the substring with its reverse string at the rightmost specific length


* @Parm str string source string from which to get the substring of the inverted string


* @Parm Lenofreversestr int Reverses the length of the string


* @Return Int has the starting position of the substring of the inverted string. Returns-1
if no substring inversion string or parameter error exists

     */


public int getposofreversestring (String str, int lenofreversestr) {


if (Lenofreversestr > str.length () | | Lenofreversestr < 1) {


return-1;


      }


for (int i = Str.length ()-lenofreversestr i >= 0; i--) {


if (Str.indexof (getreversestring (STR,I,LENOFREVERSESTR))!=-1) {


return i;


        }


      }


return-1;


    }


    /*


* @Function to get the inverted string of its substring from the specified string


* @Parm str string source string from which to get the inverted string


* @Parm s int will be reversed to the starting position of the substring in the source string


* @Parm Len Int will be reversed by the length of the substring


* @Return String that was reversed after string. Returns an empty string, such as a parameter error


     */


Public String getreversestring (string str, int s, int len) {


int tmp = s + len;


if (tmp > str.length () | | tmp < 1 | | s * Len < 0) {


return "";


      }


StringBuilder reversestr = new StringBuilder (len);


for (int i = s + len-1; I >= s; i--) {


Reversestr.append (Str.charat (i));


      }


return reversestr.tostring ();


    }


public static void Main (string[] args) {


stringx objtest = new Stringx ();


System.out.println (objtest.getmaxlenreversestring ("Arstuvywfdevutsrz"));


System.out.println (objtest.getmaxlenreversestring ("ABC"));


System.out.println (objtest.getmaxlenreversestring ("Defdedjh"));


System.out.println (objtest.getmaxlenreversestring ("Hijklkjih"));


    }


  }

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.