package cn.itsource.homeworkday14;
/**
* 字符串的倒序输出:
* 把字符串翻转过来输出 原字符串“avdkfasjks”输出效果”skjsafkdva”;
*/
//1.通过创建StringBuffer调用它的reverse()方法字符串倒序输出(比较简洁)
public class Seven {
public static void main(String[] args) {
String str6 = "abcdefgh";
StringBuffer stringbuffer =new StringBuffer(str6).reverse();
System.out.println(stringbuffer);
}
输出效果为:hgfedcba
//2.通过字符串转换为数组再遍历数组进行倒序拼接
public class Seven {
public static void main(String[] args) {
String str1 = "abcdefg";
System.out.println(reverseSort(str1));
}
static String reverseSort(String sr){
String str2 = "";
char[] a = sr.toCharArray();//字符串转换为char数组
for(int i=a.length-1;i>=0;i--){
str2 +=a[i];//从最后一个字符开始往前拼接
}
return str2;
}
}
时间: 2024-10-13 12:25:05