Java 使用 for 循环反转字符串

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

Reversing a String using a for loop

javastring

提问by Alvin Varghese

I just want to reverse a inputted String by using for loop. I have tried the following code below. [ its full of mistake i think.. cause i dont know how to convert things to array or to string in this problem ]. So anyone please help me the coding here...

我只想通过使用 for 循环来反转输入的字符串。我在下面尝试了以下代码。[我认为它充满了错误......因为我不知道如何在这个问题中将事物转换为数组或字符串]。所以任何人都请帮我在这里编码......

public class Main extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView tv = (TextView) findViewById(R.id.textView1);
        EditText input_string =(EditText) findViewById(R.id.editText1);
      final String orig = input_string.getText().toString();
        Button rev = (Button) findViewById(R.id.button1);
        rev.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                int limit = orig.length();
                for(int i=limit;i<=limit;i--)
                {

                    String[] neww = orig[i].;
                }

                tv.setText(neww);
            }}) }}

回答by Zoltorn

Something like this is what you're looking for.

像这样的东西就是你要找的。

String x = "A string";
String y = "";

for(int i = x.length()-1; i >= 0; i--){
y=y + x.charAt(i);
}

Your new string will be stored in the variable y.

您的新字符串将存储在变量 y 中。