メールアドレスを持っています
xyz@yahoo.com
メールアドレスからドメイン名を取得したい。正規表現でこれを達成できますか?
MailAddressを使用Host
すると、代わりにプロパティから取得できます
MailAddress address = new MailAddress("xyz@yahoo.com");
string host = address.Host; // host contains yahoo.com
デフォルトの答えがあなたが試みているものではない場合は、常にメールSplit
文字列の後に'@'
string s = "xyz@yahoo.com";
string[] words = s.Split('@');
words[0]
xyz
あなたが将来それを必要と
words[1]
するならyahoo.com
しかし、デフォルトの答えは確かにこれに近づくより簡単な方法です.
または、文字列ベースのソリューションの場合:
string address = "xyz@yahoo.com";
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);