java 基础 - 反转字符串
反转字符串
public class Main {
public static void main(String[] args) {
String newStr= strReverseWithArray("abc123");
System.out.println(newStr);
}
private static String strReverseWithArray(String string) {
if(string==null || string.length()==0)
return string;
int length = string.length();
char [] array=string.toCharArray();
for(int i=0;i<length;i++){
array[i]=string.charAt(length-1-i);
}
return new String(array);
}
}
运行结果:
321cba