1

I have problem with my code , I want to replace specific string to a new one but it doesn't work

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

This code what i tried , so any suggestions? Also I tried to get the index of @gmail.com and remove it but it did not work neither

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
Ebrahim ElSayed
  • 157
  • 2
  • 10
  • 3
    [Strings are immutable](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/strings/#immutability-of-string-objects). Replace doesn't change the source string. It returns a new string with the changes requested. – Steve Aug 09 '19 at 12:28
  • Yes this what happened to me , so is there another way to do what i want? – Ebrahim ElSayed Aug 09 '19 at 12:32
  • Re-read the last sentence in my comment. The answer is obvious. – Steve Aug 09 '19 at 12:33
  • 1
    Ohh okaay i got it. – Ebrahim ElSayed Aug 09 '19 at 12:34
  • Also, I don't think that it is correct to add always the @yahoo.com to your source. If you have an input like _steve@live.com_ the code above transforms the string in _steve@live.com@yahoo.com_ – Steve Aug 09 '19 at 12:39
  • `Contains` already returns a `bool` you don't need the extra if clause wrapping around it – Mong Zhu Aug 09 '19 at 13:09
  • I think for this it would be better if i get the index of @ and length of whole string and remove all strings after starting at @ and add @xyz.com(e.g) – Ebrahim ElSayed Aug 09 '19 at 13:43

2 Answers2

3

Strings are immutable, so every method in the String class does not modify the current instance but returns a new one. You have to assign this to the original variable:

sender.Text = sender.Text.Replace("@gmail.com,"@yahoo.com");

If you are interested in why strings are immutable: Why .NET String is immutable?

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
1

Something like this:

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


} 
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215