使用 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); } }
輸出:
input string :: javaguides output string :: sediugavaj
以上是java如何使用StringBuilder反轉字串的詳細內容。更多資訊請關注PHP中文網其他相關文章!