0

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!!

4 Answers 4

8

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>");
Sign up to request clarification or add additional context in comments.

Comments

4

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.

Comments

1

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.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.