1.调用reverse() 方法实现翻转
public static String reversel1(String str){
return new StringBuffer(str).reverse().toString();
}
通过new一个新的 StringBuffer 调用自带方法
2.通过字符串数组实现从尾部开始逐个逆序放入字符串中
public static String reverse2(String s)
{
char[] array = s.toCharArray(); //String转字符数组
StringBuilder reverse = new StringBuilder(); // 使用StringBuilder,更快。
for (int i = array.length - 1; i >= 0; i--){
reverse.append(array[i]);
}
return reverse.toString();
}
3.通过字符串数组实现从头部开始逐个正序放入字符串中
public static String reverse3(String s)
{
int length = s.length();
String reverse = ""; //注意这是空串,不是null
for (int i = 0; i < length; i++){
reverse = s.charAt(i) + reverse;//在字符串前面连接, 而非常见的后面
//即String="reverse",
// 放入时会"e";"s";"r";"e";"v";"e";"r";的放入String。charAt(i++)
//从尾到头正序放入
}
return reverse;
}
4.C语言中常用的方法:
public static String reverse5(String orig)
{
char[] s = orig.toCharArray();
int n = s.length - 1;
int halfLength = n / 2;
for (int i = 0; i <= halfLength; i++) {
char temp = s[i];
s[i] = s[n - i];
s[n - i] = temp;
}
return new String(s); //知道 char数组和String相互转化
}
5.利用栈的先入后出的特性实现字符串的逆转
public static String reverse5(String string) {
StringBuilder sb = new StringBuilder();
Stack s = new Stack();
for (int i = 0; i < string.length(); i++) {
s.add(string.charAt(i));
}
for (int i = 0; i < string.length(); i++) {
sb.append(s.pop());
}
return sb.toString();
}
6.通过二位进制的右移,也就是/2,实现。与方法4异曲同工
public static String reverse6(String string) {
StringBuffer sb = new StringBuffer(string);
for (int i = 0, j = sb.length() - 1; i < sb.length() >>> 1; i++, j--) {
char temp = sb.charAt(i);
sb.setCharAt(i, sb.charAt(j));
sb.setCharAt(j, temp);
}
return sb.toString();
}
所有翻转的源码
import java.util.Scanner;
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String str = sc.nextLine();
// 逆序翻转
System.out.println(reverse1(str));
}
}
public static String reverse1(String str) {
return new StringBuffer(str).reverse().toString();
}
public static String reverse2(String s) {
char[] array = s.toCharArray(); // String转字符数组
String reverse = ""; // 注意这是空串,不是null
for (int i = array.length - 1; i >= 0; i--) {
reverse += array[i];
// 即String="reverse",
// 放入时会"r";"e";"v";"e";"r";"s";"e";的放入String.charAt(String.charAt(length--))
}
return reverse;
}
public static String reverse3(String s) {
int length = s.length();
String reverse = ""; // 注意这是空串,不是null
for (int i = 0; i < length; i++) {
reverse = s.charAt(i) + reverse;// 在字符串前面连接, 而非常见的后面
// 即String="reverse",
// 放入时会"e";"s";"r";"e";"v";"e";"r";的放入String。charAt(i++)
// 从尾到头正序放入
}
return reverse;
}
public static String reverse4(String orig) {
char[] s = orig.toCharArray();
int n = s.length - 1;
int halfLength = n / 2;
for (int i = 0; i <= halfLength; i++) {
char temp = s[i];
s[i] = s[n - i];
s[n - i] = temp;
}
return new String(s); // 知道 char数组和String相互转化
}
public static String reverse5(String string) {
StringBuffer sb = new StringBuffer();
Stack s = new Stack(); // new一个栈
for (int i = 0; i < string.length(); i++) {
s.add(string.charAt(i));
// 往栈中逐个正序放入字符型string.charAt(i++);
}
for (int i = 0; i < string.length(); i++) {
sb.append(s.pop());
// 栈取出的顺序与放入的顺序相反,所以实现逆序
// length=string.length();
// 逐个增加string.charAt(length--);
}
return sb.toString();
}
public static String reverse6(String string) {
StringBuffer sb = new StringBuffer(string);
for (int i = 0, j = sb.length() - 1; i < sb.length() >>> 1; i++, j--) {
char temp = sb.charAt(i);
sb.setCharAt(i, sb.charAt(j));
sb.setCharAt(j, temp);
}
return sb.toString();
}
public static String reverse7(String string) {
StringBuffer sb = new StringBuffer();
for (int i = string.length() - 1; i >= 0; i--) {
sb.append(string.charAt(i));
}
return sb.toString();
}
}