How to split a string and assign each word to a new variable?

后端 未结 4 749
野的像风
野的像风 2020-12-21 18:21

I\'m making a credit card processing form. The field in question is Name: (first and last).

What would some C# code look like that would take the text from a text bo

相关标签:
4条回答
  • 2020-12-21 18:40

    it helps you to split a string in a part

    string FullMobileNumber = "+92-342-1234567"; string[] mobile = FullMobileNumber.Split('-'); _txtCountryCodeMobile.Text = mobile[0]; _MobileCodeDropDown.SelectedValue = mobile[1]; _txtEmployeeMobileNumber.Text = mobile[2];

    0 讨论(0)
  • 2020-12-21 18:42

    You can split the text on a character or string into a string array, and then pull the individual parts of the name out of the array. Like so:

    string[] nameParts = txtName.Text.Split(' ');
    
    string firstName = nameParts[0];
    string lastName = nameParts[1];
    

    But why not just have a separate text box for each part of the name? What if somebody puts their full name in the text box (i.e. you'd have three parts, not just two).

    0 讨论(0)
  • 2020-12-21 18:46
    string[] names = txtName.Text.Split(' ');
    string fName = names[0];
    string LName = names[1];
    
    0 讨论(0)
  • 2020-12-21 18:50

    String.Split returns an array, so... just assign from the array.

    0 讨论(0)
提交回复
热议问题