Home  |  Index  |  Dotnet4all Snippets  |  Submit resources
About  |  Mail us  
Dotnet4all Logo
String.Replace question (Thursday, December 16, 2004)
 

Found the following interesting discussion in the Newsgroups:

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.



Posted by Xander Zelders



 
Previous Posts
    - SNMP
    - Errortext in datagridrow
    - How to add a item into a textbox?
    - Adding a button (data)column to a datagrid?
    - Handling windows scroll
    - Inter-thread communication to pass a job to anothe...
    - How to read NULL values in SQL using VB .net
    - NullReferenceException occuring on installed exe
    - Marshalling question
    - Help! How do I get MS Access Field Properties in V...

Archives
    - 10/03/2004 - 10/10/2004
    - 10/10/2004 - 10/17/2004
    - 10/17/2004 - 10/24/2004
    - 10/24/2004 - 10/31/2004
    - 10/31/2004 - 11/07/2004
    - 11/21/2004 - 11/28/2004
    - 11/28/2004 - 12/05/2004
    - 12/05/2004 - 12/12/2004
    - 12/12/2004 - 12/19/2004
    - 12/19/2004 - 12/26/2004
    - 12/26/2004 - 01/02/2005
    - 01/23/2005 - 01/30/2005
    - 01/01/2006 - 01/08/2006
    - 09/24/2006 - 10/01/2006


Disclaimer & Terms of Use  | DotNet4All.Com concept & © 2004 - 2007 by  Zelders²  - Holland