2 回答

TA貢獻(xiàn)1859條經(jīng)驗(yàn) 獲得超6個(gè)贊
字符串是不可變的,因此String
類中的每個(gè)方法都不會(huì)修改當(dāng)前實(shí)例,而是返回一個(gè)新實(shí)例。您必須將其分配給原始變量:
sender.Text?=?sender.Text.Replace("@gmail.com,"@yahoo.com");

TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超10個(gè)贊
像這樣的東西:
//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 關(guān)注
- 144 瀏覽
添加回答
舉報(bào)