每次按下按钮时,如何连续将n个字符从字符串的开头移至结尾?

时间:2019-05-25 03:18:57

标签: c# string winforms

每当我按下按钮时,我都试图将字符串的第一个字符移到末尾。

我的逻辑似乎只在按下按钮后一次又一次显示第一个输出。

        string input = "";
        string manipulated = "";
        int initial;

        input = txtInput.Text;

        if (txtInput.Text == String.Empty)
        {
            MessageBox.Show("Textbox is empty, please input a string.");
        }
        else 
        {
                for (initial = 1; initial < input.Length; initial++)
                {
                    manipulated += input[initial];
                }
                manipulated += input[0];
                lblOutput.Text = manipulated.ToString();
                input = manipulated;
                manipulated = "";
        }
    }

例如如果我在文本框中输入“ 1234”并按下按钮,则输出应为“ 2341”,然后再次单击按钮后,输出应移至“ 3412” ..等等。

3 个答案:

答案 0 :(得分:0)

您要取出您的OUTPUT并将其放置在Label中...但是要继续从未更改的文本框中取出INPUT ...因此每次都得到相同的结果。< / p>

只需更改:

lblOutput.Text = manipulated.ToString();

收件人:

txtInput.Text = manipulated;

答案 1 :(得分:0)

您可以使用Substring Method

通过其他解决方案来改进代码

创建一个名为_number的新变量,并将其值设置为

public partial class Form1: Form
{
    private int _number = 1; 
    // ....
}

然后在Button事件中,您可以使用此代码替换您的代码

    private void BtnMoveText_Click(object sender, EventArgs e)
    {
        if (txtInput.Text == string.Empty)
        {
            MessageBox.Show(@"TextBox is empty, please input a string.");
            return;
        }
        if (_number > txtInput.TextLength)
            _number = 1;
        lblOutput.Text = txtInput.Text.Substring(_number) + txtInput.Text.Substring(0, _number);
        _number++;

        #region ** Depending on Microsoft **

         /*
           Substring(Int32)
             (Retrieves a substring from this instance. The substring starts at a specified character position and continues to the end of the string.)
           Parameters
               startIndex Int32
               The zero-based starting character position of a substring in this instance.
      .......................
           Substring(Int32, Int32) 
            (Retrieves a substring from this instance. The substring starts at a specified character position and has a specified length..)
           Parameters
               startIndex Int32
               The zero-based starting character position of a substring in this instance.
               length Int32                
               The number of characters in the substring.
       */
        #endregion

    }

enter image description here

答案 2 :(得分:0)

这是Basics String操作的简单示例:

private void ManipulateBtn_Click(object sender, EventArgs e)
    {
        string input = InputTxt.Text; // Read the text from you Textbox in Windos form
        if (input == string.Empty)
        {
            return;
        }
        string temp = input[0].ToString(); // Create a temp for the first char(toString) from you input
        input = input.Remove(0,1); // Remove (from you input) At Index 0 (the idex from fist char in string) 1 time) 
        input += temp; //add the firs item from you input at the end of string
        InputTxt.Text = input; // prin the result in the Textbox back.
    }

您可以看到示例SimpleStringOperation