java字符串反转

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhaoxb006/article/details/44016151

1、递归反转

 public static String reverseString(String x) {
         if (x == null || x.length() < 2)
             return x;
         else
             return reverseString(x.substring(1)) + x.charAt(0);
}

2、jdk自带的方法

public static String reverse(String str){
      return new StringBuilder(str).reverse().toString();
}

3、使用charAt()方法

public static String reverse(String str){  
        String c ="";
        for (int i = str.length() - 1; i >= 0; i--) {  
              
            c+= str.charAt(i);  
              
        }  
        return c;
}


猜你喜欢

转载自blog.csdn.net/zhaoxb006/article/details/44016151