By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,964 Members | 1,810 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,964 IT Pros & Developers. It's quick & easy.

String.Replace question

P: n/a
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("<", "&lt;")
MyTest.Replace(">", "&gt;")

'MyTest now equals "&lt;hello&gt;"

This code doesn't work the way that I would expect. Can someone shed some
light on this?

Thanks,

Craig
Nov 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Craig Buchanan" <so*****@microsoft.com> schrieb
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("<", "&lt;")
MyTest.Replace(">", "&gt;")

'MyTest now equals "&lt;hello&gt;"

This code doesn't work the way that I would expect. Can someone shed
some light on this?


Replace is a function. It returns a *new* string because the string content
can *not* be changed.

mytest = MyTest.Replace("<", "&lt;")
--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #2

P: n/a

"Craig Buchanan" <so*****@microsoft.com> wrote in message
news:ec*************@tk2msftngp13.phx.gbl...
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("<", "&lt;")
MyTest.Replace(">", "&gt;")

'MyTest now equals "&lt;hello&gt;"

This code doesn't work the way that I would expect. Can someone shed some
light on this?

Thanks,

Craig

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.
Nov 20 '05 #3

P: n/a
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
Nov 20 '05 #4

P: n/a

"Cor Ligthert" <no**********@planet.nl> wrote in message
news:Of**************@TK2MSFTNGP11.phx.gbl...
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

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.
Nov 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.