import java.util.Stack;
public class StringReverse {
public static String reverse1(String s) {
int length = s.length();
if (length <= 1)
return s;
String left = s.substring(0, length / 2);
String right = s.substring(length / 2, length);
return reverse1(right) + reverse1(left);
public static String reverse2(String s) {
int length = s.length();
String reverse = "";
for (int i = 0; i < length; i++)
reverse = s.charAt(i) + reverse;
return reverse;
}
public static String reverse3(String s) {
char[] array = s.toCharArray();
String reverse = "";
for (int i = array.length - 1; i >= 0; i--)
reverse += array[i];
return reverse;
public static String reverse4(String s) {
return new StringBuffer(s).reverse().toString();
public static String reverse5(String orig) {
char[] s = orig.toCharArray();
int n = s.length - 1;
int halfLength = n / 2;
for (int i = 0; i <= halfLength; i++) {
char temp = s[i];
s[i] = s[n - i];
s[n - i] = temp;
}
return new String(s);
public static String reverse6(String s) {
char[] str = s.toCharArray();
int begin = 0;
int end = s.length() - 1;
while (begin < end) {
str[begin] = (char) (str[begin] ^ str[end]);
str[end] = (char) (str[begin] ^ str[end]);
str[begin] = (char) (str[end] ^ str[begin]);
begin++;
end--;
return new String(str);
public static String reverse7(String s) {
char[] str = s.toCharArray();
Stack<Character> stack = new Stack<Character>();
for (int i = 0; i < str.length; i++)
stack.push(str[i]);http://www.huiyi8.com/jiaoben/
String reversed = "";javascript特效
for (int i = 0; i < str.length; i++)
reversed += stack.pop();
return reversed;
相关文章
- 10-01python3 拼接字符串的7种方法
- 10-01fastjson将json字符串转化成map的五种方法
- 10-01js冒泡排序、封装一个去除数组中重复元的的方法、封装一个将数组中的元组拼接为字符串的方法,并且弹框输出、封装一个 myQueryString 的方法,作用是将 url 种的参数解析为一个对象
- 10-01Java多线程初学者指南(7):向线程传递数据的三种方法
- 10-01做一次面向对象的体操:将JSON字符串转换为嵌套对象的一种方法
- 10-01[转]java中判断字符串是否为数字的三种方法
- 10-01总结删除文件或文件夹的7种方法-JAVA IO基础总结第4篇
- 10-01php反转字符串的三种方法
- 10-01总结删除文件或文件夹的7种方法-JAVA IO基础总结第4篇
- 10-01使用位运算、值交换等方式反转java字符串-共四种方法