kevin
kevin

Reputation: 14065

Replace method of String does not Change the String

I try to replace the file name which contains "TEMPDOCUMENTLIBRARY" with "SHAREDDOCS" in the docs (Typed Dataset). But somehow it does not replace it at all. What's wrong ?

for (int index = 0; index < docs.Document.Rows.Count; index++)
{
    if (docs.Document[index].FileName.Contains("TEMPDOCUMENTLIBRARY"))
    {
         docs.Document[index].BeginEdit();
         docs.Document[index].FileName.Replace("TEMPDOCUMENTLIBRARY", "SHAREDDOCS");
         docs.Document[index].EndEdit();
    }
}

Upvotes: 1

Views: 421

Answers (2)

M.Babcock
M.Babcock

Reputation: 18965

String.Replace does not replace in place. Try:

docs.Document[index].FileName = docs.Document[index].FileName.Replace("TEMPDOCUMENTLIBRARY", "SHAREDDOCS");

Notice in the documentation (linked above) that it returns the result of the replace.

Upvotes: 2

Adam Robinson
Adam Robinson

Reputation: 185643

Strings are immutable (meaning that the value of a given string never changes). Functions like Substring and Replace return new strings that represent the original string with the desired operations performed.

In order to achieve what you want, you need this:

docs.Document[index].FileName = 
      docs.Document[index].FileName.Replace("TEMPDOCUMENTLIBRARY", "SHAREDDOCS");

Upvotes: 4

Related Questions