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

7
  • 3
    Strings are immutable. Replace doesn't change the source string. It returns a new string with the changes requested. Commented Aug 9, 2019 at 12:28
  • Yes this what happened to me , so is there another way to do what i want? Commented Aug 9, 2019 at 12:32
  • Re-read the last sentence in my comment. The answer is obvious. Commented Aug 9, 2019 at 12:33
  • 1
    Ohh okaay i got it. Commented Aug 9, 2019 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 [email protected] the code above transforms the string in [email protected]@yahoo.com Commented Aug 9, 2019 at 12:39

2 Answers 2

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?

Sign up to request clarification or add additional context in comments.

Comments

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


} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.