【问题标题】:c# - split string inside textbox by colon and get the first string and second stringc# - 用冒号分割文本框内的字符串并获取第一个字符串和第二个字符串
【发布时间】:2017-09-27 12:08:20
【问题描述】:

我有一个文本框,当我在其中输入“alfa:rady”时,我需要将第一个字符串 (alfa) 和第二个字符串 (rady) 放入不同的文本框中。

private void textBox1_TextChanged(object sender, EventArgs e)
    {
        // what should i write?
    }

这说明了我的意思:

【问题讨论】:

  • UserName.Text = textBox1.Split(':')[0];Password.Text = textBox1.Split(':')[1];
  • 不确定您要查找的确切内容,但要使用冒号拆分字符串,您可以使用以下 string[] data = text.Split(':');。然后data[0] 包含第一个字符串,data[1] 包含第二个字符串。

标签: c# split delimiter explode colon


【解决方案1】:

您可以使用string.Split(char) 拆分这两个值:

private void textBox1_TextChanged(object sender, EventArgs e)
{
    string[] arr = textBox1.Text.Split(':');
    string username = arr[0];
    string password = arr[1];

    // Now you can use the 2 variables in other textboxes
    textUsername.Text = username;
    textPassword.Text = password;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-27
    • 2015-03-31
    • 1970-01-01
    • 1970-01-01
    • 2013-03-05
    • 1970-01-01
    • 2019-10-25
    相关资源
    最近更新 更多