2

Create an application that accepts into a textbox a person’s first given and family names (with one or more spaces between them) and when a “Separate Names” button is clicked the names are displayed separately with appropriate labelling. For example, entry of “Bob Brown” will result in an output of Family Name : Brown Given Name : Bob

here's my code:

string str=textBox1.Text;
string[] name = str.Split(' ');
MessageBox.Show("Family Name: " + name[1] + "\t" + "Given Name: " + name[0]);

how do I do this if there are more than one space in the textbox? If there is more than one space it shows up like this: Family name: Given name:bob

8 Answers 8

3

Did you try?

StringSplitOptions.RemoveEmptyEntries

stringsplitoptions

Sign up to request clarification or add additional context in comments.

Comments

2

As PawanS suggested you can use StringSplitOptions.RemoveEmptyEntries as follows:

string str=textBox1.Text;
string[] name = str.Split(' ', StringSplitOptions.RemoveEmptyEntries);
MessageBox.Show("Family Name: " + name[1] + "\t" + "Given Name: " + name[0]);

May I also remind you to confirm that the array actually contains at least two items by checking the aray's Length property to avoid getting an exception on invalid input.

Comments

2

You can remove the multiple spaces using:

string str = Regex.Replace(textBox1.Text, @"\s+", " ");

And then proceed with your code:

string[] name = str.Split(' ');
MessageBox.Show("Family Name: " + name[1] + "\t" + "Given Name: " + name[0]);

The advantage of this method is that it will remove any kind of whitespace (e.g. tabs, newlines, etc.) and replace them with a single space

Comments

1

You should remove all empty entries using StringSplitOptions in your string and split your string by yout character. For example:

string str=textBox1.Text;
var name = str.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries); 

MessageBox.Show("Family Name: " + name[1] + "\t" + "Given Name: " + name[0]);

Comments

1

Better to use LINQ perhaps?

Example:

var lastName = name.Last();
var firstNames = name.Take(name.Count()-1);
MessageBox.Show("Family Name: " + lastName + "\t" + "Given Name: " + string.Join(" ", firstNames.ToArray()));

Comments

0

You can use RegExp to replace all duplicate white space before Split string

yourStr = System.Text.RegularExpressions.Regex.Replace(yourStr ,@"\s+"," ");

Comments

0

You mean that it is possible to write input data like:

"John   Doe" //more than one space

? Try to do this:

var input = "John   Doe";
var arr = s.Split(' ').Where(x=>!string.IsNullOrWhiteSpace(x)).ToArray();
MessageBox.Show("Family Name: " + arr[1] + "\t" + "Given Name: " + arr[0]);

Comments

0
    string str=textBox1.Text;

    string firstName = str.Substring(0, str.IndexOf(' '));
    string lastName = str.Replace(" ", "").Replace(firstName, "");

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.