实现字符串的反转[汇总]java

public class Exam {
private static String f01(String a) {
   String b = "";
   char s[] = a.toCharArray();
   for (int i = s.length - 1; i >= 0; i--) {
    b += s[i];
   }
   return b;
}
public static String f02(String s) {
   int length = s.length();
   StringBuffer result = new StringBuffer(length);
   for (int i = length - 1; i >= 0; i--) {
    result.append(s.charAt(i));
   }
   return result.toString();
}
public static String f03(String s) {
   int length = s.length();
   String str = "";
   for (int i = length - 1; i >= 0; i--) {
    str += s.charAt(i);
   }
   return str;
}
public static String f04(String s) {
   int length = s.length();
   String str = "";
   for (int i = 0; i < length; i++) {
    str = s.substring(i, i + 1) + str;
   }
   return str;
}
public static String f05(String s) {
   String str = "";
   for (int i = 0; i < s.length(); i++) {
    str = s.charAt(i) + str;
   }
   return str;
}
public static String f06(String str) {
   char[] c = str.toCharArray();
   int i = str.length() - 1;
   char temp;
   for (int count = 0; count < i; count++, i--) {
    temp = c[count];
    c[count] = c[i];
    c[i] = temp;
   }
   return new String(c, 0, str.length());
}
public static String f07(String str) {
   StringBuilder sb = new StringBuilder(str);
   // StringBuffer sb = new StringBuffer(str);
   return sb.reverse().toString();
}
public static String f08(String str) {
   char[] temp = str.toCharArray();
   int length = temp.length;
   char[] recive = new char[length];
   for (int i = length; i > 0; i--) {
    recive[length - i] = temp[i - 1];
   }
   String temp2 = new String(recive);
   return temp2;
}
public static String f09(String s) {
   if (s.length() == 1) {
    return s;
   }
   String str = s.substring(s.length() - 1, s.length());
   str += f09(s.substring(0, s.length() - 1));
   return str;
}
public static void main(String[] args) {
   String a = "abcdefg";
   System.out.println(f01(a));
   System.out.println(f02(a));
   System.out.println(f03(a));
   System.out.println(f04(a));
   System.out.println(f05(a));
   System.out.println(f06(a));

你可能感兴趣的:(java,职场,休闲)