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 關注
- 148 瀏覽
添加回答
舉報