- public class ReverseString {
- public static String reverseString(String s)
{ // 反转字符串的方法 - String newStr = "" ;
// 存储反转后的结果 - int len = s.length();
// 原字符串的长度 - for (int i = len - 1; i >= 0; i--) {
// 反向遍历字符串 - newStr += s.charAt(i);
// 连接反向遍历的结果 - }
- return newStr;
- }
- public static void main(String[] args) {
- String s = "员工明星榜";
// 原字符串 - System.out.println(s);
- String reverse = ReverseString.reverseString(s); // 反转字符串
- System.out.println(reverse);
- //方法二:StringBuffer提供一个反序输出的方法,String没有提供
- StringBuffer stringBuffer = new StringBuffer(originalString);
System.out.println(stringBuffer.reverse());
- }
- }
将一个子串反序输出
最新推荐文章于 2023-07-07 06:00:00 发布