java 反转字符串的简单方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12305666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Easy way to reverse String
提问by assylias
Without going through the char sequence is there any way to reverse String
in Java
不经过字符序列有没有办法String
在Java中反转
回答by mssb
Try this,
试试这个,
String s = "responses";
StringBuilder builder = new StringBuilder(s);
System.out.println(builder.reverse());
回答by assylias
You can use the StringBuilder#reverse()
method:
您可以使用以下StringBuilder#reverse()
方法:
String reverse = new StringBuilder(originalString).reverse().toString();
回答by Kumar Vivek Mitra
Use StringBuilder's
or StringBuffer's
method... reverse()
用途StringBuilder's
或StringBuffer's
方法...reverse()
public class StringReverse
{
public static void main(String[] args)
{
String string=args[0];
String reverse = new StringBuffer(string).reverse().toString();
System.out.println("\nString before reverse: "+string);
System.out.println("String after reverse: "+reverse);
}
}
StringBuffer
is thread-safe, where as StringBuilder
is Not thread safe..... StringBuilder
was introduced from Java 1.5, as to do those operations faster which doesn't have any Concurrency to worry about....
StringBuffer
是线程安全的,因为那里StringBuilder
是不是线程安全的.....StringBuilder
从Java 1.5中引入的,因为做这些操作变得更快,其不具有任何并发到后顾之忧....
回答by lfergon
Try reverse() method:
试试 reverse() 方法:
StringBuilder stringName = new StringBuilder();
String reverse = stringName.reverse().toString();
回答by lfergon
You may use StringBuilder..
您可以使用 StringBuilder ..
String word = "Hello World!";
StringBuilder sb = new StringBuilder(word);
System.out.print(sb.reverse());
回答by nazar_art
If we have to do it:
如果我们必须这样做:
Without going through the char sequence
不经过字符序列
One easy way with iteration will be:
迭代的一种简单方法是:
public String reverse(String post) {
String backward = "";
for(int i = post.length() - 1; i >= 0; i--) {
backward += post.substring(i, i + 1);
}
return backward;
}
回答by dganesh2002
This is a way to do so using recursion -
这是一种使用递归的方法 -
public static String reverse(String s1){
int l = s1.length();
if (l>1)
return(s1.substring(l-1) + reverse(s1.substring(0,l-1)));
else
return(s1.substring(0));
}
回答by NIlesh Sharma
You can use String buffer to reverse a string.
您可以使用字符串缓冲区来反转字符串。
public String reverse(String s) {
return new StringBuffer(s).reverse().toString();
}
one more interesting way to do this is recursion.
一种更有趣的方法是递归。
public String reverse(String s) {
if (s.length() <= 1) {
return s;
}
return reverse(s.substring(1, s.length())) + s.charAt(0);
}
回答by Bob Sheehan
Using minimal API support. A simple algorithm.
使用最少的 API 支持。一个简单的算法。
static String reverse(String str) {
char[] buffer = str.toCharArray();
for (int i = 0; i < buffer.length/2; ++i){
char c = buffer[i];
buffer[i] = buffer[buffer.length-1-i];
buffer[buffer.length-1-i] = c;
}
return new String(buffer);
}
回答by Jayaram Reddy
public class RevString {
public static void main(String[] args) {
String s="jagan";
String rev="";
for (int i=s.length()-1;i>=0;i--) {
rev=rev+s.charAt(i);
}
System.out.println("Reverse String is: "+rev);
}
}