StringBuilder reverse a string Case 34

StringBuilder reverse a string Case

Case needs

  • Defines a method for reversing the string. A keyboard input string, after calling the method,
    returns the string is then reversed in the console output results.

Analysis step

1, keyboard input characters turn
2, method call, passing the string, strings obtained after reversed
3, a variable defined in the method stores the final result string
4, to traverse the character string to the variable to accumulate

public class StringBuilderExecDemo05 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(;
        String data = sc.nextLine();
        String result = reverse(data);
        System.out.println("结果:" + result);

    // 2、调用方法,传入字符串,得到反转后的字符串
    public static String reverse(String data) {

//        StringBuilder sb = new StringBuilder(data).reverse();
//        String result = sb.toString();
//        return result;

        return new StringBuilder(data).reverse().toString();

Note: no method of StringBuilder:

Published 34 original articles · won praise 16 · views 270

Guess you like