c# - Appropriate way to replace a string? -
my input abc@xyz.com
i want replace xyz.com
mnop.com
so final result abc@mnop.com
the present way
var input = "abc@xyz.com"; var output = input.split('@')[0] + "@mnop.com";
what appropriate way?any regular expression?
replace function sample (with correct exception handle)
/// <summary> /// replace email domain /// </summary> /// <param name="email"> email </param> /// <param name="newdomain"> new domain </param> /// <returns></returns> private string replacemaildomain(string email, string newdomain) { if (email == null) throw new argumentnullexception("email"); int pos = email.indexof('@'); if (pos < 0) { throw new argumentexception("invalid email", "email"); } else { return email.substring(0, pos + 1) + newdomain; } }
usage:
string email = replacemaildomain("abc@xyz.com", "mnop.com");
Comments
Post a Comment