我有“Hello World”保存在一个名为hi的字符串变量中。
我需要打印出来,但要颠倒过来。
我该怎么做呢?我知道Java中已经内置了这样一个函数。
相关:用Java反向“Hello World”字符串的每一个单词
我有“Hello World”保存在一个名为hi的字符串变量中。
我需要打印出来,但要颠倒过来。
我该怎么做呢?我知道Java中已经内置了这样一个函数。
相关:用Java反向“Hello World”字符串的每一个单词
当前回答
String string="whatever";
String reverse = new StringBuffer(string).reverse().toString();
System.out.println(reverse);
其他回答
public static void main(String[] args) {
String str = "Prashant";
int len = str.length();
char[] c = new char[len];
for (int j = len - 1, i = 0; j >= 0; j--, i++) {
c[i] = str.charAt(j);
}
str = String.copyValueOf(c);
System.out.println(str);
}
public class Test {
public static void main(String args[]) {
StringBuffer buffer = new StringBuffer("Game Plan");
buffer.reverse();
System.out.println(buffer);
}
}
使用charAt()方法
String name = "gaurav";
String reversedString = "";
for(int i = name.length()-1; i>=0; i--){
reversedString = reversedString + name.charAt(i);
}
System.out.println(reversedString);
使用toCharArray()方法
String name = "gaurav";
char [] stringCharArray = name.toCharArray();
String reversedString = "";
for(int i = stringCharArray.length-1; i>=0; i--) {
reversedString = reversedString + stringCharArray[i];
}
System.out.println(reversedString);
使用Stringbuilder的reverse()方法
String name = "gaurav";
String reversedString = new StringBuilder(name).reverse().toString();
System.out.println(reversedString);
检查https://coderolls.com/reverse-a-string-in-java/
以上所有的解决方案都太好了,但在这里,我正在使用递归编程制作反向字符串。
这对于寻找递归方法做反向字符串的人是有帮助的。
public class ReversString {
public static void main(String args[]) {
char s[] = "Dhiral Pandya".toCharArray();
String r = new String(reverse(0, s));
System.out.println(r);
}
public static char[] reverse(int i, char source[]) {
if (source.length / 2 == i) {
return source;
}
char t = source[i];
source[i] = source[source.length - 1 - i];
source[source.length - 1 - i] = t;
i++;
return reverse(i, source);
}
}
反转字符串有很多种方法。
1. 将字符串转换为字节:getBytes()方法用于将输入字符串转换为字节[]。
import java.lang.*;
import java.io.*;
import java.util.*;
class ReverseString{
public static void main(String[] args)
{
String input = "GeeksforGeeks";
byte [] strAsByteArray = input.getBytes();
byte [] result = new byte [strAsByteArray.length];
for (int i = 0; i<strAsByteArray.length; i++)
result[i] =
strAsByteArray[strAsByteArray.length-i-1];
System.out.println(new String(result));
}
}
2.将字符串转换为字符数组:用户输入需要反转的字符串。(个人建议)
import java.lang.*;
import java.io.*;
import java.util.*;
class ReverseString{
public static void main(String[] args)
{
String input = "GeeksForGeeks";
// convert String to character array
// by using toCharArray
char[] try1 = input.toCharArray();
for (int i = try1.length-1; i>=0; i--)
System.out.print(try1[i]);
}
}
3.使用ArrayList对象:使用toCharArray()内置方法将输入字符串转换为字符数组。然后,将数组的字符添加到ArrayList对象中。Java还为Collections类内置了reverse()方法。由于Collections类reverse()方法接受一个列表对象,为了反转列表,我们将传递LinkedList对象,这是一种字符列表类型。
import java.lang.*;
import java.io.*;
import java.util.*;
class ReverseString{
public static void main(String[] args)
{
String input = "Geeks For Geeks";
char[] hello = input.toCharArray();
List<Character> trial1 = new ArrayList<>();
for (char c: hello)
trial1.add(c);
Collections.reverse(trial1);
ListIterator li = trial1.listIterator();
while (li.hasNext())
System.out.print(li.next());
}
}