Index was outside the bounds of the array

I have an Excel Column “Name” that contains a first name and a last name. I need to create e-mail IDs from the names in the format

I am assigning the following variables

SplitName = row.Item(0).ToString.Split(" "c)
FirstName= SplitName(0)
LastName= SplitName(1)

However, I am getting the “Index was outside…” exception.

How do I resolve this ?

Please provide your ideas.


Hi @susbasu

Here is an example with regex:
Main.xaml (10.2 KB)

And here you can find another one with split function:

It seems that some names don’t have a space between first name and last name (maybe it’s only one of them) and that’s why you get IndexOutOfBoundsException. @ovi 's solution with regex takes the no-spaces case into account.