Antonio Mailtraq February 2016

Extract name and surname from string in c#

I need extract the name and surname from a email string.

In the database I have two type of address email working :

name.surname@xxxx.eu

Or

name.surname@xxxx.com

And I have tried this code :

    string Email1 = Email.ToString().ToUpper().Replace("@", "");
    if (Email1.Contains("XXXX.COM"))
    {
        Response.Write(Email1.ToString().Replace(".", " ").ToUpper().Remove(Email1.Length - 8, 8) + "<br />");
    }
    else
    {
        Response.Write(Email1.ToString().Replace(".", " ").ToUpper().Remove(Email1.Length - 7, 7) + "<br />");
    }

This code not working for only these addresses emails :

VINCENT.NAPOLITAIN@XXXX.EU

because the return is :

VINCENT NAPOLITAINX

Not working for :

MARK.CHAIN@XXXX.COM

because the return is :

MARK CHAINX

Not working for :

NICODEMUS.ANGELICUM@XXXX.EU

because the return is :

NICODEMUS ANGELICUMX

How to do resolve this?

Please help me, thank you so much in advance.

Answers


Patrick Hofman February 2016

One option is to find the index of the . and the index of the @ and do substrings on that:

string email = "aa.bb@cc.dd";

int periodIndex = email.IndexOf('.');
int atIndex = email.IndexOf('@');

string firstName = email.Substring(0, periodIndex);
string lastName = email.Substring(periodIndex + 1, atIndex - periodIndex - 1);

An easier way is to use a regular expression that cuts out the names.

This one will do:

(.*?)\.(.*?)@(.*)

The first capture is the first name, the second capture is the last name.


fubo February 2016

why don't you split your address by the both seperator characters @ and .

string email = "VINCENT.NAPOLITAIN@XXXX.EU"; 
string[] result = email.Split('.', '@').ToArray();
string firstname = result[0];
string lastname = result[1];


Thomas February 2016

You can use String.split():

string email = "VINCENT.NAPOLITAIN@XXXX.EU"; 
string names = email.Split('@')[0];
string name = "";
string surname = "";
if (names.Contains('.'))
{
    var nameSurname = names.Split('.');
    name = nameSurname[0]; //
    surname = nameSurname[1];
}


L-Three February 2016

Simplistic approach (based on the requirements that you specified, which seem to be a bit strange):

var test = "name.surname@xxxx.eu";
var name = test.Substring(0, test.IndexOf('@')).Replace(".", " ");

Might want to add exception handling, of course.


Miquel Coll February 2016

I would just use split in order to do it:

var nameDOTsurname = Email1.Split('@'); // This would leave in nameDOTsurname[0] the name.surname and in nameDOTsurname[1] the XXX.whatever
var name = nameDOTsurname[0].Split('.')[0];
var surname = nameDOTsurname[0].Split('.')[1];


SiD February 2016

You can use Regex Pattern:

(.+)\.(.+)(?=\@)

Explanation:

 (.+)   - Matches any character in a group 
 \.     - Matches (.) dot 
 (?=\@) - Exclude @ character

Code:

var match = Regex.Match(email, pattern);
var name = match.Groups[1].Value;
var surname = match.Groups[2].Value;


indago February 2016

This will work if your email address format sticks to firstname.secondname@wherever... here is the code that works for you...

string e = "VINCENT.NAPOLITAIN@XXXX.EU"; 
string firstname = e.Substring(0, e.IndexOf("."));
string secondname = s.Substring(s.IndexOf(".")+1, s.IndexOf("@")-s.IndexOf(".")-1);

It works for all your emails

Post Status

Asked in February 2016
Viewed 1,327 times
Voted 6
Answered 7 times

Search




Leave an answer