Java中怎么反转字符串
这篇文章给大家介绍Java中怎么反转字符串,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
目前成都创新互联公司已为上千的企业提供了网站建设、域名、网页空间、网站托管运营、企业网站设计、埇桥区网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
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 :: javaguidesoutput 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 :: javaguidesoutput 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 :: javaguidesoutput 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 :: javaguidesoutput 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 :: javaguidesoutput 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 arraypublic static String reverse(String str) {// base case: if string is null or emptyif (str == null || str.equals(""))return str;// create an empty stack of charactersStack < Character > stack = new Stack < Character > ();// push every character of the given string into the stackchar[] ch = str.toCharArray();for (int i = 0; i < str.length(); i++)stack.push(ch[i]);// start from index 0int k = 0;// pop characters from the stack until it is emptywhile (!stack.isEmpty()) {// assign each popped character back to the character arraych[k++] = stack.pop();}// convert the character array into string and return itreturn String.copyValueOf(ch);}public static void main(String[] args) {String str = "javaguides";str = reverse(str); // string is immutableSystem.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 emptyif (str == null || str.equals(""))return str;// create an empty list of charactersList < Character > list = new ArrayList < Character > ();// push every character of the given string into itfor (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 itStringBuilder 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 immutablestr = 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 arraypublic static String reverse(String str) {// return if string is null or emptyif (str == null || str.equals(""))return str;// convert string into bytesbyte[] 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 hbyte temp = bytes[l];bytes[l] = bytes[h];bytes[h] = temp;}// convert byte array back into the stringreturn new String(bytes);}public static void main(String[] args) {String str = "Java Guides";// String is immutablestr = 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 recursionprivate static String reverse(String str) {// base case: if string is null or emptyif (str == null || str.equals(""))return str;// last character + recurse for remaining stringreturn str.charAt(str.length() - 1) + reverse(str.substring(0, str.length() - 1));}public static void main(String[] args) {String str = "javaguides";// string is immutablestr = 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 variableprivate static void reverse(char[] str, int k) {// if we have reached the end of the stringif (k == str.length)return;// recurse for next characterreverse(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 emptyif (str == null || str.equals(""))return str;// convert string into a character arraychar[] A = str.toCharArray();// reverse character arrayreverse(A, 0);// convert character array into the stringreturn String.copyValueOf(A);}public static void main(String[] args) {String str = "Java Guides";// string is immutablestr = reverse(str);System.out.println("Reverse of the given string is : " + str);}}
输出:
Reverse of the given string is : sediuG avaJ
关于Java中怎么反转字符串就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
本文标题:Java中怎么反转字符串
文章位置:http://ybzwz.com/article/gcihdj.html