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