0

I have an application that updates values in documents, however, some of these documents have multiple entries of this value. due to this I have created a Do Something loop but this is just looping and is not replacing the values.

my code is as below:

do
{
    int dollarIndex = script.IndexOf("$");
    string nextTenChars = script.Substring(dollarIndex - 17, 17);
    string promptValue = CreateInput.ShowDialog(nextTenChars, "Input");
    script.Replace("$", promptValue);
}
while (script.Contains("$"));
Tom
  • 12,928
  • 2
  • 15
  • 31
  • 6
    Assuming its a string which is *immutable*: `script = script.Replace(...` – Alex K. Aug 02 '17 at 14:23
  • @AlexK, thanks Alex but this only checks the first value and does not update any of the others – Tom Aug 02 '17 at 14:25
  • 3
    `script.Replace` *returns* the new string with the replacement made - it does not change `script` in-place. You need to store the return value overwriting the original otherwise `script` always contains a `$` so `script.Contains("$")` is always `true` so your loop will run until the heat death of the universe. – Alex K. Aug 02 '17 at 14:27

1 Answers1

3

Strings are immutable, so you need to do:

script = script.Replace("$", promptValue);

Simply doing

script.Replace("$", promptValue);

Doesn't update the value of script

Stuart
  • 3,949
  • 7
  • 29
  • 58