public static String reverse1(String s) {
char[] reverseStr = new char[s.length()];
for(int i = 0; i < s.length(); i++) {
reverseStr[reverseStr.length-1-i] = s.charAt(i);
}
return new String(reverseStr);
}
public static String reverse2(String s) {
return fn(s,0,s.length()-1);
}
// 递归
public static String fn(String s, int start, int end) {
if(end < start) throw new IllegalArgumentException("endIndex must be larger than startIndex");
if(start == end) {
return s.charAt(start) + "";
}
return s.charAt(end) + fn(s,start,end-1);
}
// 使用StringBuffer中的reverse()
public static String reverse3(String s) {
return new StringBuffer(s).reverse().toString();
}
java中几种翻转字符串的办法
最新推荐文章于 2023-03-02 17:00:02 发布