在这篇文章中,我们会接头10种用Java反转字符串的要领,通过10个Java措施反转字符串。譬喻,劳务派遣管理系统,把字符串“javaguides” 反转为 “sediugavaj”。
1. 利用 + (String毗连) 操纵符
package net.javaguides.corejava.string; /** * * @author Ramesh Fadatare * */ public class ReverseWithStringConcat { public static void main(String[] args) { ReverseWithStringConcat concat = new ReverseWithStringConcat(); concat.reverseWithStringConcat("javaguides"); } private String reverseWithStringConcat(String input) { String output = new String(); for (int i = (input.length() - 1); i >= 0; i--) { output += (input.charAt(i)); } display(input, output); return output; } 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
2. 利用 StringBuilder
package net.javaguides.corejava.string; /** * * @author Ramesh Fadatare * */ 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
3. 利用 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
4. 通过互换字符反转
package net.javaguides.corejava.string; /** * * @author Ramesh Fadatare * */ public class ReverseStringWithSwaps { public static void main(String[] args) { ReverseStringWithSwaps stringWithSwaps = new ReverseStringWithSwaps(); stringWithSwaps.reverseWithSwaps("javaguides"); } public String reverseWithSwaps(String string) { final char[] array = string.toCharArray(); final int length = array.length - 1; final int half = (int) Math.floor(array.length / 2); char c; for (int i = length; i >= half; i--) { c = array[length - i]; array[length - i] = array[i]; array[i] = c; } display(string, String.valueOf(array)); return String.valueOf(array); } 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
5. 利用 XOR(^) 操纵符反转
package net.javaguides.corejava.string; /** * * @author Ramesh Fadatare * */ public class ReverseStringWithXOR { public static void main(String[] args) { ReverseStringWithXOR stringWithXOR = new ReverseStringWithXOR(); stringWithXOR.reverseWithXOR("javaguides"); } public String reverseWithXOR(String string) { final char[] array = string.toCharArray(); final int length = array.length; final int half = (int) Math.floor(array.length / 2); for (int i = 0; i < half; i++) { array[i] ^= array[length - i - 1]; array[length - i - 1] ^= array[i]; array[i] ^= array[length - i - 1]; } display(string, String.valueOf(array)); return String.valueOf(array); } 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