I have an example of an email address like this "mynameismine@hotmail.com", I just want to obtain or cut that string value and obtain this "mynameismine", I dont want to have "@hotmail.com".
Asked
Active
Viewed 2,109 times
3 Answers
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("mynameismine@hotmail.com");
string username = addr.User;
string domain = addr.Host;
username is the part before the '@' symbol and domain is the part after.

Calum
- 1,889
- 2
- 18
- 36
-
Thanks a lot I'll try with MailAddress- – Andres Oct 01 '15 at 18:01
3
var newString = emailString.Split('@').First();
split the string up by @
, grab the first item.

Jonesopolis
- 25,034
- 12
- 68
- 112
-
-
@Calum if the name contained `@` then it shouldn't be there in the first place. – DPac Sep 30 '15 at 21:27
-
1
-
2Please see this post: https://stackoverflow.com/questions/12355858/how-many-symbol-can-be-in-an-email-address – Calum Sep 30 '15 at 21:31
-
@Calum radical. you could always grab everything except the last, and concat them together. However I doubt this is a problem here. – Jonesopolis Sep 30 '15 at 21:36
-
1@Calum I stand corrected. I did not know that such oblivious address (e.g. "this$is#complete.ly@valid-%email"@example.com) is valid. – DPac Sep 30 '15 at 21:37
2
MailAddress address = new MailAddress("mynameismine@hotmail.com");
string username = address.User;
Just a class specially for these purposes.

wingerse
- 3,670
- 1
- 29
- 61