使用 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中文网其他相关文章!