String.Replace question by:Craig Buchanan
|
If I have a string variable, is there a way to get the Replace method to work on *its* contents, without having to dimenstion a second variable? Something like:
Dim MyTest as String = "<hello/>" MyTest.Replace("<", "<") MyTest.Replace(">", ">")
'MyTest now equals "<hello>"
This code doesn't work the way that I would expect. Can someone shed some light on this?
Thanks,
Craig
|
| | Reply: by:Armin Zingler
|
| | Replace is a function. It returns a *new* string because the string content can *not* be changed.
mytest = MyTest.Replace("<", "<") -- Armin
How to quote and why: http://www.plig.net/nnq/nquote.html http://www.netmeister.org/news/learn2quote.html
|
| | Reply: by:Peter van der Goes
|
| | If you need to modify existing contents, you need to use the System.Text.StringBuilder class instead of the String class. StringBuilder objects are mutable. String objects are immutable.
-- Peter [MVP Visual Developer] Jack of all trades, master of none.
|
| | Reply: by:Cor Ligthert
|
| | Hi Peter,
> If you need to modify existing contents, you need to use the > System.Text.StringBuilder class instead of the String class. StringBuilder > objects are mutable. String objects are immutable.
To my suprise as well has that no effect on the replace, the string replace is slighly faster, we did a test in this newsgroup, here the final link to that thread.
http://tinyurl.com/3bogd
Cor
|
| | Reply: by:Peter van der Goes
|
| | I was referring to the general issue of mutability. Not in particular to the Replace method, or to any performance concerns. The parameter lists for the Replace methods for the two classes explains the behavior.
|
0 Comments:
Post a Comment
<< Home