Get domain name from an email address
I have an email address
[email protected]
I want to get the domain name from the email address. Can I achieve this with Regex?
Using MailAddress you can fetch the Host
from a property instead
MailAddress address = new MailAddress("[email protected]");
string host = address.Host; // host contains yahoo.com
If Default's answer is not what you're attempting you could always Split
the email string after the '@'
string s = "[email protected]";
string[] words = s.Split('@');
words[0]
would be xyz
if you needed it in futurewords[1]
would be yahoo.com
But Default's answer is certainly an easier way of approaching this.
Or for string based solutions:
string address = "[email protected]";
string host;
// using Split
host = address.Split('@')[1];
// using Split with maximum number of substrings (more explicit)
host = address.Split(new char[] { '@' }, 2)[1];
// using Substring/IndexOf
host = address.Substring(address.IndexOf('@') + 1);