Search code examples
c#arraysstringsplitparallel-arrays

How to split a full name into two strings without using split method and storing the two strings into two parallel arrays


So my goal here is to pull the first name and last name from a full name entered into a textbox. I think I have pulled the first name correctly but I am having issues pulling the last. I do not want to use the Split method. Then I am going to store the data into the parallel arrays I have.

string name;
name = txtName.Text;
string fullName;
int indexSpace;
indexSpace = name.IndexOf(" ");
string firstName;
firstName = name.Substring(0, indexSpace);
string lastName;
lastName = ????

mFirst[mIndex] = firstName;
mIndex++;
mLast[mIndex] = lastName;
mIndex++;

Solution

  • This should do the trick. (Assuming that name follows the "firstName lastName" format - without quotes) (Assuming JavaScript or Substring adheres to the js definition)

    lastName = name.Substring(indexSpace + 1);