作者:手机用户2602879695 | 来源:互联网 | 2023-10-12 14:00
1,利用字符串的拼接(charAt()方法),把后遍历出来的放在前面即可实现反转publicstaticStringcharAtReverse(Strings){intlength
1,利用字符串的拼接(charAt()方法),把后遍历出来的放在前面即可实现反转
public static String charAtReverse (String s){
int length = s.length();
String reverse = " ";
for (int i = 0; i reverse = s.charAt(i)+reverse;//字符串中获取单个字符的字符的放法
}
return reverse;
}
2,利用字符串的拼接(toCharArray()处理成字符数组的方法),把后遍历出来的放在前面即可实现反转
public static String reverseCharArrays(String s){
char []array = s.toCharArray();//把字符串分割成单个字符的数组
String reverse = “”;
for(int i = array.length -1 ; i>=0 ; i–){//遍历数组,从后向前拼接
reverse +=array[i];
}
return reverse;
}
3,利用StringBuffer的reverse()方法
public static String reverseStringBuffer(String s){
StringBuffer sb = new StringBuffer(s);
String afterReverse = sb.reverse().toString();
return afterReverse;
}