Sankar M
Sankar M

Reputation: 4809

How to replace a string path in C#

string = @"http://192.168.30.40/mylytica/Uploads/EncryptedFile/whg_12_20_2010.pdf";
if (strEncrypt.Contains("/EncryptedFile"))
{
     strEncrypt.Replace(@"/EncryptedFile", @"/DecryptedFile");
}
iFrame_pdf.Attributes.Add("src", strEncrypt);

it doesn't work for me:( i need to replace the file path Encrypted into Decrypted

thanks in advance

Upvotes: 2

Views: 134

Answers (4)

Rasmus Faber
Rasmus Faber

Reputation: 49687

.NET Strings are immutable, so Replace() returns a new String.

Do it like this:

string strEncrypt = @"http://192.168.30.40/mylytica/Uploads/EncryptedFile/whg_12_20_2010.pdf";    
if (strEncrypt.Contains("/EncryptedFile"))    
{    
  strEncrypt = strEncrypt.Replace(@"/EncryptedFile", @"/DecryptedFile");
}

Also: remember to consider what the result should be if strEncrypt is something like "http://192.168.30.40/mylytica/Uploads/EncryptedFile/EncryptedFile.pdf".

Upvotes: 2

Sankar M
Sankar M

Reputation: 4809

string strEncrypt = @"http://192.168.30.40/mylytica/Uploads/EncryptedFile/whg_12_20_2010.pdf";
if (strEncrypt.Contains("EncryptedFile"))
{
strEncrypt = strEncrypt.Replace(@"EncryptedFile", @"DecryptedFile"); }

Upvotes: 0

DavveK
DavveK

Reputation: 173

You need to change this:

strEncrypt.Replace(@"/EncryptedFile", @"/DecryptedFile");

To this:

strEncrypt = strEncrypt.Replace(@"/EncryptedFile", @"/DecryptedFile");

From Microsoft docs on String.Replace (http://msdn.microsoft.com/en-us/library/fk49wtc1.aspx):

Note This method does not modify the value of the current instance. Instead, it returns a new string in which all occurrences of oldValue are replaced by newValue.

Upvotes: 1

JK.
JK.

Reputation: 5136

Strings are immutable so you need to assign the new string to a variable:

strEncrypt = strEncrypt.Replace(@"/EncryptedFile", @"/DecryptedFile");

Upvotes: 1

Related Questions