位置:首页 » 文章/教程分享 » Java反转字符串的10种方法(一)
在这篇文章中,我们会讨论10种用Java反转字符串的方法,通过10个Java程序反转字符串。例如,把字符串“javaguides” 反转为 “sediugavaj”。以下先介绍前面的5种方法。

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