设为首页 加入收藏

TOP

Java反转字符串的10种方法(代码段)(四)
2018-11-21 20:08:56 】 浏览:569
Tags:Java 反转 字符串 方法 代码
return builder.toString(); } public static void main(String[] args) { String str = "Java Guides"; // String is immutable str = reverse(str); System.out.println("Reverse of the given string is : " + str); } }

输出:

Reverse of the given string is : sediuG avaJ

8. 使用 Byte 数组

package net.javaguides.corejava.string;
/**
 * 
 * @author Ramesh Fadatare
 *
 */
public class ReverseStringUsingByteArray {
    // Function to reverse a string in Java using byte array
    public static String reverse(String str) {
        // return if string is null or empty
        if (str == null || str.equals(""))
            return str;
        // convert string into bytes
        byte[] bytes = str.getBytes();
        // start from the two end points l and h of the given string
        // and increment l & decrement h at each iteration of the loop
        // until two end-points intersect (l >= h)
        for (int l = 0, h = str.length() - 1; l < h; l++, h--) {
            // Swap values at l and h
            byte temp = bytes[l];
            bytes[l] = bytes[h];
            bytes[h] = temp;
        }
        // convert byte array back into the string
        return new String(bytes);
    }
    public static void main(String[] args) {
        String str = "Java Guides";
        // String is immutable
        str = reverse(str);
        System.out.println("Reverse of the given string is : " + str);
    }
}

输出:

Reverse of the given string is : sediuG avaJ

9. 使用 substring() 方法

package net.javaguides.corejava.string;
/**
 * 
 * @author Ramesh Fadatare
 *
 */
public class UsingSubStringFunction {
    // Function to reverse a string in Java using recursion
    private static String reverse(String str) {
        // base case: if string is null or empty
        if (str == null || str.equals(""))
            return str;
        // last character + recurse for remaining string
        return str.charAt(str.length() - 1) + reverse(str.substring(0, str.length() - 1));
    }
    public static void main(String[] args) {
        String str = "javaguides";
        // string is immutable
        str = reverse(str);
        System.out.println("Reverse of the given string is : " + str);
    }
}

输出:

Reverse of the given string is : sediugavaj

10. 使用递归

package net.javaguides.corejava.string;
/**
 * 
 * @author Ramesh Fadatare
 *
 */
public class UsingRecursion {
    static int i = 0;
    // Recursive function to reverse a string in Java using static variable
    private static void reverse(char[] str, int k) {
        // if we have reached the end of the string
        if (k == str.length)
            return;
        // recurse for next character
        reverse(str, k + 1);
        if (i <= k) {
            char temp = str[k];
            str[k] = str[i];
            str[i++] = temp;
        }
    }
    public static String reverse(String str) {
        // base case: if string is null or empty
        if (str == null || str.equals(""))
            return str;
        // convert string into a character array
        char[] A = str.toCharArray();
        // reverse character array
        reverse(A, 0);
        // convert character array into the string
        return String.copyValueOf(A);
    }
    public static void main(String[] args) {
        String str = "Java Guides";
        // string is immutable
        str = reverse(str);
        System.out.println("Reverse of the given string is : " + str);
    }
}

输出:

Reverse of the given string is : sediuG avaJ

String 相关文章

  1. Java String Class API 指南
  2. Java String 常量池指南
  3. Java String 方法实例
  4. Java String 最佳实践
  5. String 特殊操作符实例
  6. String 比较函数实例
  7. String 字符提取函数
  8. String 搜索函数实例
  9. String 修改函数实例
  10. Java StringBuffer Class API 指南
  11. Java StringBuilder Class 方法
  12. Java StringBuffer Class 方法
  13. Java StringBuilde
首页 上一页 1 2 3 4 下一页 尾页 4/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Java调试检查表 下一篇SpringBoot系列一:SpringBoot入门

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目