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 {
    //1、键盘录入字符转
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("请您输入一个字符串:");
        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:
https://blog.csdn.net/qq_41005604/article/details/105273068

Published 34 original articles · won praise 16 · views 270

Guess you like

Origin blog.csdn.net/qq_41005604/article/details/105298211