Using Java or JS to implement string reverse

Source: Internet
Author: User

A bit is my study, found and learned some of the methods, the great gods have a lot of guidance:

1, using JS to achieve, this personal method feels more direct

1 //js for reverse output2<script type= "Text/javascript" >3$(function(){4Alert (1)5     6 varstr = ' You are a big bad man ';7 varNewstr = Str.split (""). Reverse (). Join ("");8 alert (NEWSTR);9 })Ten  One</script>

2, using the ToCharArray () of the string class to implement the reverse method

1 ImportJavax.swing.JOptionPane;2 3  Public classTest {4     5 //using the ToCharArray () of the string class to implement the reverse method6      Public Static voidMain (string[] args) {7 String orgstring;8String resultstring = "";9 //pop-up box prompt messageTenOrgstring =joptionpane.showinputdialog ("Please enter content that needs to be reversed!") "); One         Char[] chararray=Orgstring.tochararray (); A          for(inti = chararray.length-1; I >=0; i--) { -resultstring+=Chararray[i]; -         } theJoptionpane.showmessagedialog (NULL, resultstring, "Reverse String", joptionpane.information_message); -     } -      -}

3, using the substring () method provided by the String class plus recursion

1 import Javax.swing.JOptionPane;2 3 Public class Test {4 5     //using the substring () method provided by the string class plus recursion6 7public staticvoidresultstring (String str) {8         if(str.length () = = 1) {9 System.out.print (str);Ten}Else { OneString subString1 = str.substring (0, Str.length ()-1); AString subString2 = str.substring (Str.length ()-1); - System.out.print (subString2); - resultstring (subString1); the         } -     } -  -public staticvoidMain (string[] args) { +  - String originalstring; +originalstring = Joptionpane.showinputdialog ("Please enter content that needs to be reversed:"); A resultstring (originalstring); at  -     } -}

Using Java or JS to implement string reverse

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.