Java Fundamentals Hardening Case of string inversion of 46:stringbuffer class

Source: Internet
Author: User

1. Case Demo:

1  Packagecn.itcast_07;2 3 ImportJava.util.Scanner;4 5 /*6 * Reverse the string7  */8  Public classStringBufferTest3 {9      Public Static voidMain (string[] args) {Ten         //Keyboard input Data OneScanner sc =NewScanner (system.in); ASystem.out.println ("Please enter data:"); -String s =sc.nextline (); -  the         //Mode 1: concatenation with string -String S1 =Myreverse (s); -System.out.println ("S1:" +s1); -         //Mode 2: Use StringBuffer's reverse () function +String s2 =MyReverse2 (s); -System.out.println ("S2:" +S2); +     } A  at     //using StringBuffer's reverse () function -      Public Staticstring MyReverse2 (string s) { -         //stringbuffer sb = new StringBuffer (); -         //Sb.append (s); -  -         //stringbuffer sb = new StringBuffer (s); in         //sb.reverse (); -         //return sb.tostring (); to  +         //Simple Version -         return NewStringBuffer (s). Reverse (). toString (); the     } *  $     //make a concatenation with a stringPanax Notoginseng      Public Staticstring Myreverse (string s) { -String result = ""; the  +         Char[] CHS =S.tochararray (); A          for(intx = chs.length-1; x >= 0; x--) { the             //char ch = chs[x]; +             //result + = ch; -Result + =Chs[x]; $         } $  -         returnresult; -     } the}

Operation Result:

Java Fundamentals Hardening Case of string inversion of 46:stringbuffer class

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.