在这篇文章中,我们会讨论10种用java反转字符串的方法,通过10个java程序反转字符串。例如,把字符串“javaguides” 反转为 “sediugavaj”。
1. 使用 + (string连接) 操作符
package net.javaguides.corejava.string; /** * * @author ramesh fadatare * */ public class reversewithstringconcat { public static void main(string[] args) { reversewithstringconcat concat = new reversewithstringconcat(); concat.reversewithstringconcat("javaguides"); } private string reversewithstringconcat(string input) { string output = new string(); for (int i = (input.length() - 1); i >= 0; i--) { output += (input.charat(i)); } display(input, output); return output; } private void display(string input, string output) { system.out.println(" input string :: " + input); system.out.println(" output string :: " + output); } }
输出:
input string :: javaguides output string :: sediugavaj
2. 使用 stringbuilder
package net.javaguides.corejava.string; /** * * @author ramesh fadatare * */ public class reversewithstringbuilderbuiltinmethod { public static void main(string[] args) { reversewithstringbuilderbuiltinmethod builtinmethod = new reversewithstringbuilderbuiltinmethod(); builtinmethod.reversewithstringbuilderbuiltinmethod("javaguides"); } public string reversewithstringbuilderbuiltinmethod(string string) { final stringbuilder builder = new stringbuilder(string); display(string, builder.reverse().tostring()); return builder.reverse().tostring(); } private void display(string input, string output) { system.out.println(" input string :: " + input); system.out.println(" output string :: " + output); } }
输出:
input string :: javaguides output string :: sediugavaj
3. 使用 string charat 方法
package net.javaguides.corejava.string; /** * * @author ramesh fadatare * */ public class reversewithstringchatat{ public static void main(string[] args) { reversewithstringchatat reversewithstringbuilder = new reversewithstringchatat(); reversewithstringbuilder.reversewithstringbuilder("javaguides"); } public string reversewithstringchatat(string string) { final stringbuilder builder = new stringbuilder(); for (int i = (string.length() - 1); i >= 0; i--) { builder.append(string.charat(i)); } display(string, builder.tostring()); return builder.tostring(); } private void display(string input, string output) { system.out.println(" input string :: " + input); system.out.println(" output string :: " + output); } }
输出:
input string :: javaguides output string :: sediugavaj
4. 通过交换字符反转
package net.javaguides.corejava.string; /** * * @author ramesh fadatare * */ public class reversestringwithswaps { public static void main(string[] args) { reversestringwithswaps stringwithswaps = new reversestringwithswaps(); stringwithswaps.reversewithswaps("javaguides"); } public string reversewithswaps(string string) { final char[] array = string.tochararray(); final int length = array.length - 1; final int half = (int) math.floor(array.length / 2); char c; for (int i = length; i >= half; i--) { c = array[length - i]; array[length - i] = array[i]; array[i] = c; } display(string, string.valueof(array)); return string.valueof(array); } private void display(string input, string output) { system.out.println(" input string :: " + input); system.out.println(" output string :: " + output); } }
输出:
input string :: javaguides output string :: sediugavaj
5. 使用 xor(^) 操作符反转
package net.javaguides.corejava.string; /** * * @author ramesh fadatare * */ public class reversestringwithxor { public static void main(string[] args) { reversestringwithxor stringwithxor = new reversestringwithxor(); stringwithxor.reversewithxor("javaguides"); } public string reversewithxor(string string) { final char[] array = string.tochararray(); final int length = array.length; final int half = (int) math.floor(array.length / 2); for (int i = 0; i < half; i++) { array[i] ^= array[length - i - 1]; array[length - i - 1] ^= array[i]; array[i] ^= array[length - i - 1]; } display(string, string.valueof(array)); return string.valueof(array); } private void display(string input, string output) { system.out.println(" input string :: " + input); system.out.println(" output string :: " + output); } }
输出:
input string :: javaguides output string :: sediugavaj
6. 使用堆栈
package net.javaguides.corejava.string; import java.util.stack; /** * * @author ramesh fadatare * */ public class reversestringusingstack { // function to reverse a string in java using a stack and character array public static string reverse(string str) { // base case: if string is null or empty if (str == null || str.equals("")) return str; // create an empty stack of characters stack < character > stack = new stack < character > (); // push every character of the given string into the stack char[] ch = str.tochararray(); for (int i = 0; i < str.length(); i++) stack.push(ch[i]); // start from index 0 int k = 0; // pop characters from the stack until it is empty while (!stack.isempty()) { // assign each popped character back to the character array ch[k++] = stack.pop(); } // convert the character array into string and return it return string.copyvalueof(ch); } public static void main(string[] args) { string str = "javaguides"; str = reverse(str); // string is immutable system.out.println("reverse of the given string is : " + str); } }
输出:
reverse of the given string is : sediugavaj
7. 使用 collections reverse() 方法
package net.javaguides.corejava.string; import java.util.arraylist; import java.util.collections; import java.util.list; /** * * @author ramesh fadatare * */ public class reversestringusingcollectionsreversemethod { // function to reverse a string in java using collections.reverse() public static string reverse(string str) { // base case: if string is null or empty if (str == null || str.equals("")) return str; // create an empty list of characters list < character > list = new arraylist < character > (); // push every character of the given string into it for (char c: str.tochararray()) list.add(c); // reverse list using java.util.collections reverse() collections.reverse(list); // covert arraylist into string using stringbuilder and return it stringbuilder builder = new stringbuilder(list.size()); for (character c: list) builder.append(c); return builder.tostring(); } public static void main(string[] args) { string str = "java guides"; // string is immutable str = reverse(str); system.out.println("reverse of the given string is : " + str); } }
输出:
reverse of the given string is : sediug avaj
8. 使用 byte 数组
package net.javaguides.corejava.string; /** * * @author ramesh fadatare * */ public class reversestringusingbytearray { // function to reverse a string in java using byte array public static string reverse(string str) { // return if string is null or empty if (str == null || str.equals("")) return str; // convert string into bytes byte[] bytes = str.getbytes(); // start from the two end points l and h of the given string // and increment l & decrement h at each iteration of the loop // until two end-points intersect (l >= h) for (int l = 0, h = str.length() - 1; l < h; l++, h--) { // swap values at l and h byte temp = bytes[l]; bytes[l] = bytes[h]; bytes[h] = temp; } // convert byte array back into the string return new string(bytes); } public static void main(string[] args) { string str = "java guides"; // string is immutable str = reverse(str); system.out.println("reverse of the given string is : " + str); } }
输出:
reverse of the given string is : sediug avaj
9. 使用 substring() 方法
package net.javaguides.corejava.string; /** * * @author ramesh fadatare * */ public class usingsubstringfunction { // function to reverse a string in java using recursion private static string reverse(string str) { // base case: if string is null or empty if (str == null || str.equals("")) return str; // last character + recurse for remaining string return str.charat(str.length() - 1) + reverse(str.substring(0, str.length() - 1)); } public static void main(string[] args) { string str = "javaguides"; // string is immutable str = reverse(str); system.out.println("reverse of the given string is : " + str); } }
输出:
reverse of the given string is : sediugavaj
10. 使用递归
package net.javaguides.corejava.string; /** * * @author ramesh fadatare * */ public class usingrecursion { static int i = 0; // recursive function to reverse a string in java using static variable private static void reverse(char[] str, int k) { // if we have reached the end of the string if (k == str.length) return; // recurse for next character reverse(str, k + 1); if (i <= k) { char temp = str[k]; str[k] = str[i]; str[i++] = temp; } } public static string reverse(string str) { // base case: if string is null or empty if (str == null || str.equals("")) return str; // convert string into a character array char[] a = str.tochararray(); // reverse character array reverse(a, 0); // convert character array into the string return string.copyvalueof(a); } public static void main(string[] args) { string str = "java guides"; // string is immutable str = reverse(str); system.out.println("reverse of the given string is : " + str); } }
输出:
reverse of the given string is : sediug avaj
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。
如对本文有疑问, 点击进行留言回复!!
MyBatis——动态SQL语句——if标签和where标签复合使用
网友评论