473,804 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Does String.Replace always recreate a new string object?

If I use something like this,

string html = "<h1>C# is great</h1>";
Console.WriteLi ne(html.Replace ("&lt;","<").Re place("&gt;","> "));

Does this recreate new string objects two times, even though it has
nothing to replace, and it is OK to return the same string object?

I know, regular expression is a more delicate way to do this, but it
always is kind of too complicated to me.
Thank you.
Feb 22 '08 #1
3 1968

"Sin Jeong-hun" <ty*******@gmai l.comwrote in message
news:65******** *************** ***********@j28 g2000hsj.google groups.com...
If I use something like this,

string html = "<h1>C# is great</h1>";
Console.WriteLi ne(html.Replace ("&lt;","<").Re place("&gt;","> "));

Does this recreate new string objects two times, even though it has
nothing to replace, and it is OK to return the same string object?
No, Replace (string, string) does not always create a new string.

Hilton
Feb 22 '08 #2
This returns true because the CLR uses string interning to optimize
string handling.
Actually it seems to work even if the strings aren't interned, suggesting it
is instead a "I didn't update anything, return the original reference"
optimisation; since it is an internal-call, it is hard to check...

Anyways, s1 and s2 come out as ref-equals, even though it isn't interned.

Marc

string s0 = "foobar"; // expect this to be interned
Console.WriteLi ne("s0: {0}", s0);
Console.WriteLi ne("s0 interned: {0}", (string.IsInter ned(s0) !=
null));
string s1 = new string('a',5); // don't expect this to be
interned
Console.WriteLi ne("s1: {0}", s1);
Console.WriteLi ne("s1 interned: {0}", (string.IsInter ned(s1) !=
null));
string s2 = s1.Replace("b", "c");
Console.WriteLi ne("s2: {0}", s1);
Console.WriteLi ne("ref-equals: {0}", object.Referenc eEquals(s1,
s2));
Console.WriteLi ne("s1 interned: {0}", (string.IsInter ned(s1) !=
null));
Console.WriteLi ne("s2 interned: {0}", (string.IsInter ned(s2) !=
null));
Feb 22 '08 #3
(I'm going to have to find a faster nntp feed ;-p)
Feb 22 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2754
by: BigDadyWeaver | last post by:
I am using the following code in asp to define a unique and unpredictable record ID in Access. <% 'GENERATE UNIQUE ID Function genguid() Dim Guid guid = server.createobject("scriptlet.typelib").guid guid=Left(guid,instr(guid,"}")) genguid=guid
10
5806
by: DataBard007 | last post by:
Hello Access Gurus: I use Win98SE and Access97. I just built a simple Access97 application which holds all contact information for my personal contacts, such as first name, last name, address, city, state, etc. When the user wants to search for a particular record, he does two things: 1. On the form is a text box on which he enters the text he is searching for.
12
1843
by: Zoury | last post by:
Hi there! :O) I need to replace all the accentued character of a string by it's non-accentued-character equivalent. 1. is there a way to do so without iterating trought the entire string and replacing character, with the Convert or Encoding class for example? 2. if not, how can you actually replace a specific character in a string without recreating a new string?
5
2888
by: rogsonl | last post by:
My computer was moved last week, and the company changed the network groups we work on. As a result, one of the main benefits from Whidbey (database connectivity) no longer works. Situation: 1. I open a new project based on a windows form 2. I click on "data" and then "Add New Data Source" then I click on next within the wizard 3. I have 3 choices Database, Local Database, Web service and Object. 4. I choose Database, click next and get...
4
23007
by: Lauren Wilson | last post by:
Hi folks, We have a need to replace sub strings in certain message text. We use the Office Assistant to display help and often use the imbedded formatting commands. Those of you who have used them know they look like this: "{cf 5}" or "{cf 0}" or "{ul 1}" or "{ul 0}", etc. The commonality they have is that they are always 6 charters long and always begin and end with curly brackets. This all works great if the user is running the...
14
4867
by: Anoop | last post by:
Hi, I am new to this newsgroup and need help in the following questions. 1. I am workin' on a GUI application. Does C# provides Layout Managers the way Java does to design GUI? I know that it can be done using the designer but I intentionally don't want to use that. The one reason is that you cannot change the code generated by the designer. The other could be that you have more free hand and control to design your GUI. 2....
4
23054
by: moondaddy | last post by:
I need to edit the text in many files so I'm writing a small routine to do this. First I have a method that loops through all the files in a directory and passes the full file path to another method (ReadFile). I'm going to use this to edit the file paths in all of my WMP play lists. When I run this the string variable is not being changed using the replace method. Here's what I have: Private Sub ReadFile(ByVal path As String)
14
3498
by: webEater | last post by:
I have a problem, it's not browser specific, and I don't get a solution. I have an (X)HTML document, I show you a part of it: .... <!--<div class="pad">--> <div id="eventImages"><img src="" id="eventImage0" class="eventImage"><img src="" id="eventImage1" class="eventImage"></div>
3
324
by: Sorrow | last post by:
I was watching a javascript presentation and one of the slides had the following code: <code> String.prototype.supplant = function( o ) { return this.replace(/{(*)}/g, function( a, b ) { var r = o; return typeof r === 'string' ? r : a;
0
10595
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10343
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10088
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9169
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7633
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5668
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.