0

I have a string "{Day} - is a victory day. {Day} - is my birthday";

How to replace {Day} with different numbers? I try:

string text = "{Day} - is a victory day. {Day} - is my birthday";
if (text.Contains("{Day}"))
    text = text.Replace("{Day}", xNet.Rand.Next(1, 30).ToString());

But day is the same in 2 replacements.

halfer
  • 19,824
  • 17
  • 99
  • 186
opkrb
  • 13
  • 2

6 Answers6

5

Use Regex.Replace with MatchEvaluator delegate:

string text = "{Day} - is a victory day. {Day} - is my birthday";
text = Regex.Replace(text, @"\{Day\}", m => xNet.Rand.Next(1, 30).ToString());
tukaef
  • 9,074
  • 4
  • 29
  • 45
2

String.Replace takes a string as a "replace with" parameter and replaces all occurrences with that. For example, in your case, Rand may generate the string 19 once, which is then passed in to Replace and will replace all occurrences of {Day}.

If you want it to generate a new replacement for each match, you can use Regex.Replace instead, it can take a function/lambda that is called once per match to generate the replacement string;

text = Regex.Replace(text, Regex.Escape("{Day}"),
        match => xNet.Rand.Next(1,30).ToString());
Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
0

That happens because xNet.Rand.Next(1, 30).ToString() only gets called once. That is, when text.Replace(..) fires.

To replace each occurance with a different number, you could do something like:

string searchString = "{Day}";
int foundIndex = text.IndexOf(searchString);
while (foundIndex > -1)
{
  text = text.Substring(0, foundIndex) 
           + xNet.Rand.Next(1, 30).ToString() 
           + text.Substring(foundIndex + searchString.Length);
  foundIndex = text.IndexOf(searchString);
}

Untested, but this should give you a clue how to solve it.

pyrocumulus
  • 9,072
  • 2
  • 43
  • 53
0

According to the related (String.Replace) MSDN entry:

Returns a new string in which all occurrences of a specified Unicode character or String in the current string are replaced with another specified Unicode character or String.

http://msdn.microsoft.com/en-us/library/system.string.replace.aspx

Notice that it replaces all occurences. On your current code, you are replacing both occurences with the same randomly generated value.

Two possible solutions:

  • Split the original string into two: Replace the value in the first, then in the second: and then concatenate both again.
  • Rename the first {Day} parameter to, say, {Day0}; and the second to {Day1}. Then generate a random value for each.
OnoSendai
  • 3,960
  • 2
  • 22
  • 46
0

Yes, because Rand.Next is evaluated only once, so the same value will be replaced.

You can do it with regex or manually:

string text = "{Day} - is a victory day. {Day} - is my birthday";
var sb = new StringBuilder(text);
while (text.Contains("{Day}"))
{
  int p=text.IndexOf("{Day}");
  sb.Remove(p,5);
  sb.Insert(p, xNet.Rand.Next(1, 30).ToString());
  text = sb.ToString();
}
0

This is probably not the best way to do what you are trying to do... BUT you at least need to understand that you are only calling the text.Replace() method once, and it is working as expected. I.E. replacing each instance of {Day} with the number the method generates. If you want them to be two different values you need to replace two different values.

    string text = "{Day} - is a victory day.  {Day2} - is my birthday";
    text = text.Replace("{Day}", xNet.Rand.Next(1, 30).ToString());
    text = text.Replace("{Day2}", xNet.Rand.Next(1, 30).ToString());
Evan L
  • 3,805
  • 1
  • 22
  • 31
  • P.S. I realize this is sloppy... just trying to keep the answer in the context of the question. Which is also sloppy ;) – Evan L May 21 '13 at 21:26