使用 String charAt 方法
package net.javaguides.corejava.string; /** * * @author Ramesh Fadatare * */ public class ReverseWithStringChatAt{ public static void main(String[] args) { ReverseWithStringChatAt reverseWithStringBuilder = new ReverseWithStringChatAt(); reverseWithStringBuilder.reverseWithStringBuilder("javaguides"); } public String reverseWithStringChatAt(String string) { final StringBuilder builder = new StringBuilder(); for (int i = (string.length() - 1); i >= 0; i--) { builder.append(string.charAt(i)); } display(string, builder.toString()); return builder.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如何使用String charAt方法反转字符的详细内容。更多信息请关注PHP中文网其他相关文章!