2 回答
TA贡献1859条经验 获得超6个赞
字符串是不可变的,因此String
类中的每个方法都不会修改当前实例,而是返回一个新实例。您必须将其分配给原始变量:
sender.Text = sender.Text.Replace("@gmail.com,"@yahoo.com");
TA贡献1820条经验 获得超10个赞
像这样的东西:
//DONE: we should check for null
//DONE: it's Yahoo if it ends on @yahoo.com (not contains)
public static bool IsYahoo(TextBox sender) =>
sender != null &&
sender.Text.TrimEnd().EndsWith("@yahoo.com", StringComparison.OrdinalIgnoreCase);
public static bool IsGmail(TextBox sender) =>
sender != null &&
sender.Text.TrimEnd().EndsWith("@gmail.com", StringComparison.OrdinalIgnoreCase);
public static void InsertYahoo(TextBox sender) {
if (null == sender)
throw new ArgumentNullException(nameof(sender));
if (IsYahoo(sender))
return;
// Uncomment, In case you want to change gmail only
//if (!IsGmail(sender))
// return;
// If we have an eMail like bla-bla-bla@somewhere
int p = sender.Text.LastIndexOf('@');
// ... we change somewhere to yahoo.com
if (p > 0)
sender.Text = sender.Text.Substring(0, p) + "@yahoo.com";
}
- 2 回答
- 0 关注
- 119 浏览
添加回答
举报