Zora Blank Zora Blank - 1 year ago 84
C# Question

C# Coding using strings in order to find a full name

I am using Visual studio to create a C#

windows form
that helps me find the suffix,first and last name of the user. I am using
to find the first space and split from there but it only gives me from the first space onward. if the user input " Mr. Donald duck " I can not manage to make it work in the situation.

"Mr. -5 spaces- Donald -5spaces- Duck"

the code doesn't read past the first space.

any suggestions?

Answer Source

You should always use String.Trim() function. (To remove leading and trailing white-space from string) when you deal with user input as a string.

string s = " Mr. Donald duck ";
// Split string on spaces.
// ... This will separate all the words.
string[] words = s.Trim().Split(' ');
//.....check size of array.
if(words.Length ==3)
    string suffix=words[0];
    string firstname=words[1];
    string lastname=words[2];

I am not getting -5 in your question but hope this will help.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download