文章目录
苏小妍一面让我撕代码题反转字符串,我没做出来,哭了 # 反转字符串
344. 反转字符串
把字符串 hello 转化为 olleh
package Threadlocal;
public class test
{
public static void main(String[] args) {
String s="hello";
char[] chars=s.toCharArray();
int l=0;
int r=s.length()-1;
while(l<r){
char temp = chars[l];
chars[l++] = chars[r];
chars[r--] = temp;
}
System.out.println(String.valueOf(chars));
}
}
方法2
StringBuilder sb=new StringBuilder(s);
String a= sb.reverse().toString();
System.out.println(a);