-4

I have an example of an email address like this "[email protected]", I just want to obtain or cut that string value and obtain this "mynameismine", I dont want to have "@hotmail.com".

0

3 Answers 3

5

If you're going to be manipulating email addresses you're best off using the MailAddress class in the System.Net.Mail namespace:

MailAddress addr = new MailAddress("[email protected]");
string username = addr.User;
string domain = addr.Host;

username is the part before the '@' symbol and domain is the part after.

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

1 Comment

Thanks a lot I'll try with MailAddress-
3
var newString = emailString.Split('@').First();

split the string up by @, grab the first item.

6 Comments

This wouldn't work if the name contained an '@' symbol.
@Calum if the name contained @ then it shouldn't be there in the first place.
@Calum doubt that would be a valid email address then
@Calum I stand corrected. I did not know that such oblivious address (e.g. "this$is#complete.ly@valid-%email"@example.com) is valid.
|
2

using System.Net.Mail;

MailAddress address = new MailAddress("[email protected]");
string username = address.User;

Just a class specially for these purposes.

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.