473,404 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

String.Replace question

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
4 1133
"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

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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Roose | last post by:
How can I do a "".replace operation which tells me if anything was actually replaced? Right now I am doing something like: if searchTerm in text: text = text.replace( searchTerm, 'other' ) ...
4
by: higabe | last post by:
Three questions 1) I have a string function that works perfectly but according to W3C.org web site is syntactically flawed because it contains the characters </ in sequence. So how am I...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
2
by: Dan Schumm | last post by:
I'm relatively new to regular expressions and was looking for some help on a problem that I need to solve. Basically, given an HTML string, I need to highlight certain words within the text of the...
12
by: Jeff S | last post by:
In a VB.NET code behind module, I build a string for a link that points to a JavaScript function. The two lines of code below show what is relevant. PopupLink = "javascript:PopUpWindow(" &...
9
by: Peter Row | last post by:
Hi, I know this has been asked before, but reading the threads it is still not entirely clear. Deciding which .Replace( ) to use when. Typically if I create a string in a loop I always use a...
5
by: djc | last post by:
I need to prepare a large text database field to display in an asp.net repeater control. Currently I am replacing all chr(13)'s with a "<br/>" and it works fine. However, now I also want to be able...
11
by: billpaterson2006 | last post by:
I've been working on some code to search for specific textstrings and act upon them insome way. I've got the conversion sorted however there is 1 problem remaining. I am trying to work out how...
3
by: MLH | last post by:
Back in mid-2003, lucason posted a question about removing punctuation chars from a string. Suggested code was posted using Replace function. Could the FN below be easily modified for use with A97...
10
by: TC | last post by:
Hey All, I have strings in currency. For example: "$1.29" What is the best way to convert them to decimal formatted like: "1.29" Thanks, TC
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.