Java中把一个字符串实现反转【杭州多测师_王sir】【杭州多测师】
Java中把一个字符串实现反转
/** * 使用StringBuilder的reverse()方法将字符串反转 */ public static void main(String[] args) { StringBuilder sb = new StringBuilder("ABCDE牛"); System.out.println(sb.reverse()); }
public static String reverseTestThree(String s) { StringBuffer sb = new StringBuffer(); for (int i = s.length() - 1; i >= 0; i--) { sb.append(s.charAt(i)); } return sb.toString(); }
public static String reverseTestSix(String s) { if (s.length() <= 1) { return s; } return reverseTestSix(s.substring(1)) + s.substring(0, 1); }