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".
3 Answers
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.
1 Comment
Andres
Thanks a lot I'll try with MailAddress-
var newString = emailString.Split('@').First();
split the string up by @, grab the first item.
6 Comments
Calum
This wouldn't work if the name contained an '@' symbol.
DPac
@Calum if the name contained
@ then it shouldn't be there in the first place.Jonesopolis
@Calum doubt that would be a valid email address then
Calum
Please see this post: stackoverflow.com/questions/12355858/…
DPac
@Calum I stand corrected. I did not know that such oblivious address (e.g. "this$is#complete.ly@valid-%email"@example.com) is valid.
|
MailAddress address = new MailAddress("[email protected]");
string username = address.User;
Just a class specially for these purposes.