challengeAccepted
challengeAccepted

Reputation: 7590

String. Replace function issue!

I think i need some help for the string replace function. This one do not replace and gives me same thing back. What might be the issue, guys?

FormattedURl = mysite.com/Merchant.aspx?1=lkdflfdfgj3242

lblclick.Text.Replace("<a class=linkclass href=http://www.mysite.com/ target=_blank >   </a>", 
"<a class=linkclass href=" + FormattedURL1 + "target=_blank ></a>");

Thank you in advance!!

Upvotes: 0

Views: 250

Answers (4)

Donnie
Donnie

Reputation: 46903

Don't forget that string is c# is immutable. Thus, there is no way that Replace could change it in place. Instead, it returns a new copy with the replacement done.

Upvotes: 1

Brian Rasmussen
Brian Rasmussen

Reputation: 116401

String is immutable, so all functions on string return new instances. Thus to see the effect of the function you must assign the result.

lblclick.Text = lblclick.Text.Replace("<a class=linkclass href=http://www.mysite.com/ target=_blank >   </a>", "<a class=linkclass href=" + FormattedURL1 + "target=_blank ></a>");

From the documentation of Replace:

Returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string.

Upvotes: 4

Aaron McIver
Aaron McIver

Reputation: 24713

It does not modify the existing instance, it returns an instance with the changes.

From MSDN...

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.

Therefore you need to store the returned value and set it on your label or simply set your label to the returned value.

Upvotes: 1

jimplode
jimplode

Reputation: 3502

You need to assign the text back into the variable or control.

The replace function will return a string, not change the current instance.

lblclick.Text = lblclick.Text.Replace("<a class=linkclass href=http://www.mysite.com/ target=_blank >   </a>", 
"<a class=linkclass href=" + FormattedURL1 + "target=_blank ></a>");

Upvotes: 8

Related Questions