java将字符串反转几种方法总结
主要方法:
(1)返回字符串的长度。
package com.cn;
public class Test {public static void main(String[] args) {String str="hello world";System.out.println("字符串str的长度为:"+str.length());}
}
结果:
(2)返回字符串的子字符串
package com.cn;
public class Test {public static void main(String[] args) {String str="hello world";System.out.println("字符串str的长度为:"+str.length());System.out.println("子字符串为:"+str.substring(3));System.out.println("子字符串为:"+str.substring(3,7));}
}
结果:
(3)字符串转化为字符数组。
package com.cn.reverse;
public class Test {public static void main(String[] args) {String s="asfrefg";char[] str = s.toCharArray(); //转化为字符数组for(int i = 0; i <s.length(); i++) {System.out.print(str[i]+" ");}}
}
结果:
1、递归方法。
package com.cn.reverse;
public class Demo1 {public static void main(String[] args) {System.out.println("字符串反转的结果为:"+str.reverse1("abcdef"));}
}
class str1{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);}
}
结果:
2、 通过 charAt(int index)返回char值进行字符串拼接。
package com.cn.reverse;
public class Demo2 {public static void main(String[] args) {System.out.println("字符串反转的结果为:"+str2.reverse2("frgsgcde"));}
}
class str2{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;}
}
结果:
3、把字符串转换成字符数组倒叙拼接然后返回值。
package com.cn.reverse;
public class Demo3 {public static void main(String[] args) {System.out.println("字符串反转的结果为:"+str3.reverse3("123dfefvg"));}
}
class str3{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;}
}
结果:
4、调用StringBuffer中的reverse方法。
package com.cn.reverse;
public class Demo4 {public static void main(String[] args) {System.out.println("字符串反转的结果为:"+str4.reverse4("frhj45a")); }
}
class str4{public static String reverse4(String s) {return new StringBuffer(s).reverse().toString();}
}
结果:
5、把字符串转换成字符数组首位对调位置。
package com.cn.reverse;
public class Demo5 {public static void main(String[] args) {// TODO Auto-generated method stubSystem.out.println("字符串反转的结果为:"+str5.reverse5("123456")); }
}
class str5{public static String reverse5(String str) {char[] s = str.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);}
}
结果:
6、
package com.cn.reverse;
public class Demo6 {public static void main(String[] args) {// TODO Auto-generated method stubSystem.out.println("字符串反转的结果为:"+str6.reverse6("frhs5p5")); }
}
class str6{public static String reverse6(String s) {char[] str = s.toCharArray(); //转化为字符数组int begin = 0;int end = s.length() - 1; //6while(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);}
}
结果:
7、借助栈的特点:先进后出。
package com.cn.reverse;
import java.util.Stack;
public class Demo7 {public static void main(String[] args) {// TODO Auto-generated method stubSystem.out.println("字符串反转的结果为:"+str7.reverse7("sfghv4sl")); }
}
class str7{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]);String reversed = "";for (int i = 0; i < str.length; i++)reversed += stack.pop();return reversed;}
}
结果: