Search code examples
c#stringsplitdelimiter

c# - split string inside textbox by colon and get the first string and second string


I have a text box that when I type into it such as "alfa:rady," I need the first string (alfa) and second string (rady) put into different text boxes.

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

This illustrates what I mean:

this one


Solution

  • You can split the 2 values using 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;
    }