java 使用子字符串向后显示字符串

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

Displaying a string backwards using substring

javasubstring

提问by First Lady

I have here a method that displays a string backward.

我在这里有一个向后显示字符串的方法。

public static String ReverseStr(String backward) {
       String newString = "";
       for (int i=0; i<backward.length(); i++) {
          newString = backward.charAt(i) + newString;
       }
       return newString;
}

It works properly but I'd like to do it in another way, that is, using a for loop, getting each character in the string using substring method with two parameters, starting with the last character then concatenates that one-character substring with the new string each time through the loop.

I tried this:

它工作正常,但我想以另一种方式进行,即使用for 循环,使用substring 带有两个参数的方法获取字符串中的每个字符,从最后一个字符开始,然后将该单字符子字符串与新字符串连接起来每次通过循环。

我试过这个:

public static String ReverseStr(String backward) {
       String newString = "";
       for (int i=0; i>backward.length(); i--) {
          String subChar = backward.substring(backward.length()-1);
          newString += subChar;
       }
       return newString;
}

But when I run the program, it displays nothing. Please help me fix the code. Thank you very much!

但是当我运行程序时,它什么也没显示。请帮我修复代码。非常感谢你!

回答by

If this is not a homework, then use StringBuilder class and reverse() method.

如果这不是作业,那么使用 StringBuilder 类和 reverse() 方法。

http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/StringBuilder.html#reverse()

http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/StringBuilder.html#reverse()

If this is a homework do it by yourself.

如果这是家庭作业,请自己做。

回答by Tomasz Nurkiewicz

The loop never executes due to wrong control condition:

由于错误的控制条件,循环永远不会执行:

for (int i=0; i>backward.length(); i--)

It starts from 0and continues until iis greater than length(which is never true as lengthis always at least 0). You probablymeant:

它从开始0并一直持续到i大于length(这永远不会正确length,至少总是如此0)。你可能的意思是:

for (int i=backward.length(); i>=0; i--)

Once you manage to fix your code, consider using StringBuilderinstead of concatenating to String. And once you manage to use StringBuilder, you'll discover handy reverse()method:

一旦您设法修复您的代码,请考虑使用StringBuilder而不是连接到String. 一旦你设法使用StringBuilder,你会发现方便的reverse()方法:

return new StringBuilder(backward).reverse().toString();

回答by Tomer

Your for loop is messed up, it will never run since you have the wrong condition, change it to:

你的 for 循环搞砸了,它永远不会运行,因为你有错误的条件,将其更改为:

 for (int i=backward.length(); i>=0; i--)

回答by Kaleend

class Test
{
    public static void main(String[] args)
    {
        String strtorev = "Test Code";
        String newString = "";
        int len = strtorev.length();
        System.out.println(len);
        for(int i =0;i<len;i++)
        {
            String subChar = strtorev.substring(strtorev.length()-1-i,strtorev.length()-i);
            newString += subChar;
            System.out.println(newString);
        }
    }
}

回答by Jayaraj Chevery

We can use recursion to reverse a String

我们可以使用递归来反转字符串

public class ReverseStringTest {
    public static String reverseString(String str) {
        if (str.isEmpty()) {
            return str;
        } 
        //Calling Function Recursively
        return reverseString(str.substring(1)) + str.charAt(0);
    }

    public static void main(String[] args) {
        System.out.println(reverseString("java"));
    }
}

回答by Lion

You should StringBuilder. The following code should work as you expect.

你应该StringBuilder。以下代码应该按您的预期工作。

public static String ReverseStr(String backward)
{
   String newString = "";
   StringBuilder sb=new StringBuilder(backward);
   for (int i=0; i>backward.length(); i--) //backward.length() may be replaced with sb.length()
   {
      String subChar = sb.charAt(i); //Replace `substring()` with `charAt()`
      newString += subChar;
   }
   return newString;
}

回答by Its Me

public static String ReverseStr(String backward) {

       String newString = "";

       for (int i=backward.length()-1; i>=0; i--) 
          newString+ = backward.charAt(i);

       return newString;
}