Gunakan StringBuilder
package net.javaguides.corejava.string; /** * * @author yisu * */ public class ReverseWithStringBuilderBuiltinMethod { public static void main(String[] args) { ReverseWithStringBuilderBuiltinMethod builtinMethod = new ReverseWithStringBuilderBuiltinMethod(); builtinMethod.reverseWithStringBuilderBuiltinMethod("javaguides"); } public String reverseWithStringBuilderBuiltinMethod(String string) { final StringBuilder builder = new StringBuilder(string); display(string, builder.reverse().toString()); return builder.reverse().toString(); } private void display(String input, String output) { System.out.println(" input string :: " + input); System.out.println(" output string :: " + output); } }
Output:
input string :: javaguides output string :: sediugavaj
Atas ialah kandungan terperinci Bagaimana untuk membalikkan rentetan menggunakan StringBuilder dalam java. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!