Java 反串法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20312214/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 00:59:52  来源:igfitidea点击:

Reverse string method

javastringreverse

提问by user3003605

I am trying to solve the following problem but how do write the method that accepts String as an argument?

我正在尝试解决以下问题,但是如何编写接受 String 作为参数的方法?

Write a method named printReversethat accepts a String as an argument and prints the characters in the opposite order. If the empty string is passed as an argument, the method should produce no output. Be sure to write a main method that convincingly demonstrates your program in action. Do not use the reverse method of the StringBuilderor StringBufferclass!

编写一个名为的方法printReverse,它接受一个字符串作为参数并以相反的顺序打印字符。如果空字符串作为参数传递,则该方法不应产生任何输出。请务必编写一个 main 方法,以令人信服地演示您的程序的运行情况。不要使用StringBuilderorStringBuffer类的反向方法 !

So far I have solved it in a easier manner:

到目前为止,我已经以更简单的方式解决了它:

import java.util.Scanner;

class ReverseString
{
public static void main(String args[])
{
  String original, reverse = "";
  Scanner in = new Scanner(System.in);

  System.out.println("Enter a string to reverse");
  original = in.nextLine();

  int length = original.length();

  for ( int i = length - 1 ; i >= 0 ; i-- )
     reverse = reverse + original.charAt(i);

  System.out.println("Reverse of entered string is: "+reverse);
 }
}

回答by duffymo

Something like this:

像这样的东西:

public class StringUtils {
    public static String reverse(String forward) {
        String result = "";
        // Put your code here
        return result;
    }
}

回答by Maroun

I highly recommend you to go through a basic tutorial.

我强烈建议您阅读基本教程

You can simply do:

你可以简单地做:

private static String myReverse(String str) {
    String reverse = "";
    int length = str.length();
    for( int i = length - 1 ; i >= 0 ; i-- ) {
       reverse = reverse + str.charAt(i);
    }
    return reverse;
}

And in your main, you simply:

在您的 中main,您只需:

String reversed = myReverse(in.nextLine());

Note that the methodis staticbecause you're referring to it from a staticmanner(mainmethod). If you don't want it to be static, you'll have to access it via an object.

请注意,该方法static因为您从静态方式main方法)引用它。如果您不希望它是static,则必须通过object访问它。

Also note that it's a good practice to always have curly brackets for forloops, even if it contains a single line.

另请注意,始终为for循环使用大括号是一种很好的做法,即使它包含一行也是如此。

回答by Kareem Hashem

private static void printReverse (String org)
{

  StringBuffer buffer = new StringBuffer(org);
  String reversedStr = buffer.reverse().toString();
  System.out.println("The reverse of the string \""
            + str + "\" is \"" + reversedStr + "\".");

}

in the main call the function

在主调用函数

printReverse(original);

回答by Sage

how do write the method that accepts String as an argument?

如何编写接受 String 作为参数的方法?

public static String reverse(String forward) {
   char[] strChar = forward.toCharArray();
   String reverse = "";

   for( int i = strChar.length - 1 ; i >= 0 ; i-- ) 
       reverse = reverse + strChar[i];

   return reverse;
}

But for large string appending character with +operator can be inefficient. And reversing string with above approach will result in wrong for uni-code mismatches. As it reverse the code units but not character. There is actually a built-in support available to reverse a string using StringBuilderwhich works correctly:

但是对于带有+运算符的大字符串附加字符可能效率低下。使用上述方法反转字符串将导致 uni-code 不匹配的错误。因为它反转了代码单元而不是字符。实际上有一个内置支持可用于反转字符串,使用StringBuilder它可以正常工作:

public static String reverse(String forward) {
   StringBuilder builder = new StringBuilder(forward);
   String reverse = builder.reverse().toString();
   return reverse;    
}

回答by parag.rane

try this:

尝试这个:

private static String reverseString(String str)
{
    String revString = "";

    for(int i=str.length()-1;i>=0;i--)
    {
        revString = revString + str.charAt(i);
    }
    return revString;
}

回答by cynthia vidolo

public class Dowhile {
  package dowhile;
  public static void main(String[] args) {
     // TODO code application logic here
     String message="i love java programming";
     int msglength = message.length();

     int index=msglength-1;
     while(index>=0)
     {
        System.out.print(message.charAt(index));
        index--;
     }  
  }
}