Search code examples
c#visual-studiomanipulate

How to seperate string to other TextBox


I am using Visual Studio, I type in the first textbox for example Name:First/age:22 but if try other name it will not work. How to manipulate the string inputted by the user. Any advise what is the use for this scenario except this substring?

 private void btnManipulate_Click(object sender, EventArgs e)
        {
            string stringmanipulate = inputString.Text;
            resultName.Text = stringmanipulate.Substring(5, 5);
            resultAge.Text = stringmanipulate.Substring(15, 2);
        }

This is my example output

[![enter image description here][1]][1]

If I type this

[![enter image description here][2]][2]

This is the error

[![enter image description here][3]][3]


Solution

  • You can use String.Split()

    //stringArray[0] will be your original string up until / - "Name:blabla"
    //stringArray[1] will be your original string after / - "age:22"
    var stringArray = stringmanipulate.Split('/');
    

    After that you can use String.Split() again but with the ':' character and apply the same logic