开发者

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

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 tex开发者_运维知识库t box and split it, then assign each word (in this case first and last name) into two new strings?

E.g. txtName.Text = "John Doe"

After split

string fName = "John";

string LName = "Doe";


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).


string[] names = txtName.Text.Split(' ');
string fName = names[0];
string LName = names[1];


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];


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜