标签:blog ar os 使用 java for div log bs
对于使用Java字符串反转有以下几种实现:
public class Reverse { public static String reverse1(String str) { if (str == null || str.length() <= 1) return str; return new StringBuilder(str).reverse().toString(); } public static String reverse2(String str) { if (str == null || str.length() <= 1) return str; return reverse2(str.substring(1)) + str.charAt(0); } public static String reverse3(String str) { if (str == null || str.length() <= 1) return str; char[] ca = new char[str.length()]; for (int i = 0; i < str.length(); i++) { ca[i] = str.charAt(str.length() - i - 1); } return new String(ca); } public static void main(String[] args) { String s = "hello world"; System.out.println(reverse1(s)); System.out.println(reverse2(s)); System.out.println(reverse3(s)); } }
标签:blog ar os 使用 java for div log bs
原文地址:http://www.cnblogs.com/elvalad/p/4072380.html