string inversion in Java

Source: Internet
Author: User

The method:
public void Convertstr (String str) {
Convert a String object to a StringBuffer class object that can be changed
Then call the reverse () method of the StringBuffer class to implement the inversion
String strreverse=new stringbuffer (str). reverse (). toString ();
System.out.println (StrReverse);

}
Other methods:
public void Convertstr (String str) {

for (int i=str.length () -1;i>=0;i--)
{
Output one character per reverse
System.out.print (Str.charat (i));
}
}
A less desirable approach: trouble

public void Convertstr (String str) {

String strnew= "";
String [] s=new string[str.length ()];
for (int x=0;x<str.length (); x + +)
{
S[x]=str.substring (x,x+1);
}
for (int x=str.length () -1;x>=0;x--)
{
STRNEW+=S[X];
}
System.out.println (strnew);
}


Test:

public class Exchange {
public static void Main (string[] args) {
String str= "ABC";
New Exchange (). CONVERTSTR (str);

}


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.