关于java中字符串反转的几种方法

1.通过StringBuffer进行字符串反转

String str = "I Love U";

StringBuffer buffer = new StringBuffer(str);

str = buffer.reverse().toString();

System.out.println(buffer.reverse().toString());

2.通过Collections工具类进行反转
char [] charStr = (str.toCharArray());
List list = new ArrayList();
for(int i=0;ilist.add(charStr[i]);
}
Collections.reverse(list);
for(Iterator it = list.iterator();it.hasNext();){
System.out.print(it.next());
}

3.使用apache lang3包中的ArrayUtils工具包
char [] charStr = str.toCharArray();
ArrayUtils.reverse(charStr);
System.out.println(new String(charStr));

4.自己实现转换的方法
char[] charStr = str.toCharArray();
char c;
for(int i=0,j=charStr.length-1;ic = charStr[i];
charStr[i] = charStr[j];
charStr[j] = c;
}
System.out.println(new String(charStr));

你可能感兴趣的:(java基础)