-1

I am making a mail program where I would like to replace **NAME** with the name of the reciepient. I already tried this:

string mail = "hello **NAME**, how are you doing?";
mail.Replace("**NAME**", Reciepient.Name);

but it isn't working and just leaves the string untouched.

does anyone have any ideas?

aloisdg
  • 22,270
  • 6
  • 85
  • 105
TDM
  • 73
  • 1
  • 1
  • 12

2 Answers2

0

Replace() returns the string modified. You have to assigned it to mail or to a new variable.

string mail = "hello **NAME**, how are you doing?";
mail = mail.Replace("**NAME**", Reciepient.Name);
aloisdg
  • 22,270
  • 6
  • 85
  • 105
-1

use regex

    string mail = "hello **NAME**, how are you doing?";
    string pattern = @"\bNAME\b";
    string replace =  Reciepient.Name;
    string result = Regex.Replace(mail, pattern, replace);
    Console.WriteLine(result);

result : hello **test**, how are you doing?

Saif
  • 2,611
  • 3
  • 17
  • 37
  • This is not what was asked. – Adrian Sep 03 '18 at 14:11
  • Counter example: `"Keep this - NAME - change that **NAME**"`: 1st `NAME` should be preserved intact; `string pattern = @"\*\*NAME\*\*";` – Dmitry Bychenko Sep 03 '18 at 14:12
  • the stars can maintain the same and just replace the word `NAME` .. as I can see from his comment that he just wants to replace the `name` by the new value – Saif Sep 03 '18 at 14:18
  • just wondering why got downvote! – Saif Sep 03 '18 at 14:19
  • The downvote is because the question reads: `I would like to replace **NAME** with the name of the reciepient`... not "How do I replace the string between the tags". – Adrian Sep 03 '18 at 14:19
  • but this line `string replace = Reciepient.Name;` already shows how to replace so is that not answering the question! – Saif Sep 03 '18 at 14:22
  • No? This line shows how to assign a string value of `Name` to another variable `replace`... clearly OPs problem is that he did not reassign the string back to the variable.... – Adrian Sep 03 '18 at 14:23