亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用方法替換特定字符串?

如何使用方法替換特定字符串?

C#
HUX布斯 2023-08-20 09:49:08
我的代碼有問題,我想將特定字符串替換為新字符串,但它不起作用public void InsertYahoo(TextBox sender){    if (IsGmail(sender))    {        ReplaceGmail(sender);    }    else if(IsYahoo(sender))    {        return;    }    else    {        sender.Text +="@yahoo.com";    }}public bool IsYahoo(TextBox sender){    if (sender.Text.Contains("@yahoo.com")    {        return true;    }    else    {         return false;    }}public bool IsGmail(TextBox sender){     if (sender.Text.Contains("@gmail.com")     {        return true;    }    else    {         return false;    }}public void ReplaceGmail(TextBox sender){    sender.Text.Replace("@gmail.com, "@yahoo.com");}這段代碼是我嘗試過的,所以有什么建議嗎?我還嘗試獲取 @gmail.com 的索引并將其刪除,但它也不起作用
查看完整描述

2 回答

?
慕絲7291255

TA貢獻1859條經驗 獲得超6個贊

字符串是不可變的,因此String類中的每個方法都不會修改當前實例,而是返回一個新實例。您必須將其分配給原始變量:

sender.Text?=?sender.Text.Replace("@gmail.com,"@yahoo.com");
查看完整回答
反對 回復 2023-08-20
?
拉莫斯之舞

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";




查看完整回答
反對 回復 2023-08-20
  • 2 回答
  • 0 關注
  • 148 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號