Java 倒序输出
/*
* 字符串abcdefg,要求按逆序输出为gfedcba
*/
public class ReverseSort {
public static String reverseSort(String str) {
String str2 = "";
for (int i = str.length() - 1; i > -1; i--) {
str2 += String.valueOf(str.charAt(i));
}
return str2;
}
public static void main(String[] args) {
String str = "abcdefg";
String sortedStr = reverseSort(str);
System.out.println(sortedStr);
}
}
- //使用reverse()方法
- public class reverseTest {
- public static void main(String[] args) {
- String originalString = "abcdefg";
- StringBuffer stringBuffer = new StringBuffer(originalString);
- System.out.println(stringBuffer.reverse());
- }
- }
String a = "abc";
String[] b = a.split("");
String c = "";
for (int i=b.length;i>0;i--){
c+=b[i];
}