class Demo{
public static String reverse(String s){
//若参数为空或去掉两边空格为"" 返回值为参数自身
if (s==null||s.trim()=="") {
return s;
}
//获得字符串的字符数组
char[] chs = s.toCharArray();
//获得字符串的长度
int length = s.length();
//循环交换数组中对称位置的字符
for (int i = 0; i < length/2; i++) {
char c = chs[i];
chs[i]=chs[length-1-i];
chs[length-1-i]=c;
}
//返回由交换过的数组构成的新字符串
return new String(chs);
}
public static void main(String[] args) {
System.out.println(reverse("FVVWJ"));
}
}
Java 自己写reverse方法
最新推荐文章于 2021-10-11 23:56:49 发布