What are the methods of java string inversion
This article mainly introduces the methods of java string inversion, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
1. Use the reverse method public static String reverseTestOne (String s) {return new StringBuffer (s). Reverse (). ToString () of stringBuffer or stringBuilder to split the string into char array public static String reverseTestTwo (String s) {char [] charArray = s.toCharArray (); int start = 0, end = charArray.length-1; for (; start)
< end; start++, end--) { charArray[start] ^= charArray[end];//采用异或 charArray[end] ^= charArray[start]; charArray[start] ^= charArray[end]; } return new String(charArray); } 两数交换除了用异或还能用其他方法,请移步不借助第三变量实现两数交换 3、stringBuffer倒序拼接 public static String reverseTestThree(String s) { StringBuffer sb = new StringBuffer(); for (int i = s.length() - 1; i >= 0; iMel -) {sb.append (s.charAt (I));} return sb.toString ();} 4. Public static String reverseTestFour (String s) {StringBuffer sb = new StringBuffer (); Stack stack = new Stack (); for (int I = 0; I < s.length ()) While +) {stack.push (s.charAt (I));} while (! stack.isEmpty ()) {/ / stack returns the top value of the stack and deletes the value sb.append (stack.pop ());} return sb.toString ();} 5, binary transposition reversal public static String reverseTestFive (String s) {int start = 0 Int end = s.length ()-1; char [] charArray = s.toCharArray (); while (start < end) {/ / Exchange head and tail chars char c = charArray [end]; charArray [end] = charArray [start]; charArray [start] = c; start++; end-- } return new String (charArray);} 6. Cut recursive inversion public static String reverseTestSix (String s) {if (s.length ())