title: Search and Replace within a string
body: <p>I have a string as below.</p>
  
  <pre><code>string data = "A := B;\nC:=D";
  </code></pre>
  
  <p>The string has to be replaced with <code>SET</code> statement as below.</p>
  
  <pre><code>data = "SET A=B;\nSET C=D"
  </code></pre>
  
  <p>It should replace <code>:=</code> with =<code>and insert a</code>SET` statement. </p>
  
  <p>I came out with an algorithm as below but it's not working when I have multiple <code>:=</code>.</p>
  
  <p>Is there any other easiest and efficient way to handle this? Perhaps using RegEx?</p>
  
  <pre><code>private string AddSETStatement(string script)
  {
          script = script.Replace(": =", ":=");
          script = script.Replace(":=", ":= ");
          script = script.Replace(" :=", ":= ");
          int found = script.IndexOf(":=");
          while (found &gt; -1)
          {
              int foundspace = -1;
              for (int index = found; index &gt;= 0; index--)
              {
                  string temp = script.Substring(index, 1);
                  if (temp == " ")
                  {
                      foundspace = index;
                      break;
                  }
              }
              script = script.Remove(found, 1);
              if (foundspace == -1)
              {
                  script = "SET " + script;
              }
              else
              {
                  script = script.Insert(foundspace, " SET ");
              }
              found = script.IndexOf(":=");
          }
  
          return script.Trim();
  }
  </code></pre>
  
  <p>Any help would be appreciated.</p>