Skip to content

String.Replace(oldStr, newStr)???!!!!

rooz

Asked Feb 9, 2006, 7:12 AM EST2 answers
Share
LinkedInX
string sqlQuery = "Some huge automated text";
sqlQuery.Replace("S", "");

this "Replace" for some weird reason doesn't work!!! I tried debegging this in "command window". and here's the message from the command window:

? sqlQuery.Replace("as", "bas")
error: 'sqlQuery.Replace' does not exist


I have included "System" in my code -> "using System;". I have tried:
string and String... I'm all out of ideas... what am I missing??

thanx in advance!

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    You need to reassign the replacement to the string variable.

    string sqlQuery = "Some huge automated text";
    sqlQuery = sqlQuery.Replace("S", "");
    0
  • 0

    That is because that the contents of a string object cannot be altered. When you replace some characters using Replace -method, you can see that the method returns a string, that is a altered copy of the original. All operations of the System.String-class work the same way.

    If you want to have a "alterable" string, you can use System.Text.StringBuilder class. Its contects can be direclty changed, thus it is more efficient in those cases, when you need to do lots of string manipulation.

    0