Java反转字符串的10种⽅法
在这篇⽂章中,我们会讨论10种⽤Java反转字符串的⽅法,通过10个Java程序反转字符串。例如,把字符串“javaguides” 反转为 “sediugavaj”。
1. 使⽤ + (String连接) 操作符
package java.string;
/**
*
* @author Ramesh Fadatare
*
*/
public class ReverseWithStringConcat {
public static void main(String[] args) {
ReverseWithStringConcat concat = new ReverseWithStringConcat();
}
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 java.string;
/**
*
* @author Ramesh Fadatare
*
*/
public class ReverseWithStringBuilderBuiltinMethod {
public static void main(String[] args) {
ReverseWithStringBuilderBuiltinMethod builtinMethod = new ReverseWithStringBuilderBuiltinMethod();
}
public String reverseWithStringBuilderBuiltinMethod(String string) {
final StringBuilder builder = new StringBuilder(string);
display(string, verse().toString());
verse().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 java.string;
/**
*
public class ReverseWithStringChatAt{
public static void main(String[] args) {
ReverseWithStringChatAt reverseWithStringBuilder = new ReverseWithStringChatAt(); verseWithStringBuilder("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, String());
String();
}
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 java.string;
/**
*
* @author Ramesh Fadatare
*
*/
public class ReverseStringWithSwaps {
public static void main(String[] args) {
ReverseStringWithSwaps stringWithSwaps = new ReverseStringWithSwaps(); verseWithSwaps("javaguides");
}
public String reverseWithSwaps(String string) {
final char[] array = CharArray();
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 java.string;
/**
*
* @author Ramesh Fadatare
*
*/
public class ReverseStringWithXOR {
public static void main(String[] args) {
ReverseStringWithXOR stringWithXOR = new ReverseStringWithXOR(); verseWithXOR("javaguides");
}
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 java.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 = CharArray();
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
pyValueOf(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 java.string;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
*java valueof
* @author Ramesh Fadatare
public class ReverseStringUsingCollectionsReverseMethod {
// Function to reverse a string in Java verse() 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: CharArray())
list.add(c);
// reverse list using java.util.Collections reverse() verse(list);
// covert ArrayList into String using StringBuilder and return it StringBuilder builder = new StringBuilder(list.size());
for (Character c: list)
builder.append(c);
String();
}
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 java.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 = Bytes();
// 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 java.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 java.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 = CharArray();
/
/ reverse character array
reverse(A, 0);
// convert character array into the string
pyValueOf(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
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论