Reputation:
Can someone explain why I get different results from these two statements? I thought that reassigning the value to the same variable would result in the value I get in the above example. What am I missing here?
_body.Replace("##" + _variableName + "##",
templateVariables[_variableName])
Hello pitty ##LastName##,
_body = _body.Replace("##" + _variableName.ToUpper() + "##",
templateVariables[_variableName])
Hello ##FirstName## ##LastName##,
Upvotes: 2
Views: 215
Reputation: 5282
If I understand this correctly: Your first statement is not assigning the return value, since replace returns a new instance of the string replaced.
_body = _body.Replace("##" + _variableName + "##",
templateVariables[_variableName]);
should fix you there.
The second instance you have the variable getting replace changed ToUpper() and the actual string containing mixed cased values.
Your string should be
Hello ##FIRSTNAME## ##LASTNAME##,
Upvotes: 2
Reputation: 994787
You've got a call to .ToUpper()
in your second example. Is this what's causing the behaviour you see?
Upvotes: 2
Reputation: 46595
Strings are immutable, so the Replace function doesn't modify the string it is called on. You need to assign it again like you did in your second example.
And as other people have pointed out, the ToUpper call will ensure that variable names don't match.
Upvotes: 7