473,785 Members | 2,772 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Copy Stringbuilder

Hi,

in my Application i have to make a Copy of a StringBuilder,
so that, if the new old instance is changed, the you instance will not be
affected.

I could do something like
new StringBuilder(m yStringBuilder. ToString())

but as i understand correct this would twice copy the content of my
StringBuilder
1. from StringBuilder to string
2. from string to StringBuilder.

Im using StingBuilder to prevent superfluous copying of large strings.

Christof
Nov 17 '05 #1
5 8497
Christof,

What you are doing is correct. The ToString() method does not copy the
string data in the StringBuilder instance. It just references the data.

Best Regards
Johann Blake

Nov 17 '05 #2
"Johann Blake" <jo*********@ya hoo.com> schrieb im Newsbeitrag
news:11******** *************@g 14g2000cwa.goog legroups.com...
Christof,

What you are doing is correct. The ToString() method does not copy the
string data in the StringBuilder instance. It just references the data.

Best Regards
Johann Blake


Can't imagine that.
If it was so, then the content of the string Instance could be changed by
the StringBuilder.

Best Regards
Christof Nordiek
Nov 17 '05 #3
Christof,
My understanding is that StringBuilder.T oString does not copy the string
data, it just references the data as Johann stated.

I understand that when you go to modify the StringBuilder data after calling
it's ToString, it then copies the existing data & "starts over".

Seeing as I (and I suspect most other developers) normally "throw" the
StringBuilder away after calling its ToString, this is a good algorithm to
follow.

Hope this helps
Jay
"Christof Nordiek" <cn@nospam.de > wrote in message
news:Oj******** ********@TK2MSF TNGP14.phx.gbl. ..
| "Johann Blake" <jo*********@ya hoo.com> schrieb im Newsbeitrag
| news:11******** *************@g 14g2000cwa.goog legroups.com...
| > Christof,
| >
| > What you are doing is correct. The ToString() method does not copy the
| > string data in the StringBuilder instance. It just references the data.
| >
| > Best Regards
| > Johann Blake
| >
|
| Can't imagine that.
| If it was so, then the content of the string Instance could be changed by
| the StringBuilder.
|
| Best Regards
| Christof Nordiek
|
|
Nov 17 '05 #4
Hi Jay,
thanks for responding.

"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> schrieb im
Newsbeitrag news:eo******** ******@TK2MSFTN GP09.phx.gbl...
Christof,
My understanding is that StringBuilder.T oString does not copy the string
data, it just references the data as Johann stated.

I understand that when you go to modify the StringBuilder data after
calling
it's ToString, it then copies the existing data & "starts over".
.... Yes, that could be (but does someone knows it for sure?).
But that wouldn't solve my problem, since in most cases I will have to reuse
the
original as StringBuilder, so the copy will occur anyway.
....
"Christof Nordiek" <cn@nospam.de > wrote in message
news:Oj******** ********@TK2MSF TNGP14.phx.gbl. ..
| "Johann Blake" <jo*********@ya hoo.com> schrieb im Newsbeitrag
| news:11******** *************@g 14g2000cwa.goog legroups.com...
| > Christof,
| >
| > What you are doing is correct. The ToString() method does not copy the
| > string data in the StringBuilder instance. It just references the
data.
| >
| > Best Regards
| > Johann Blake
| >
|
| Can't imagine that.
| If it was so, then the content of the string Instance could be changed
by
| the StringBuilder.
|
| Best Regards
| Christof Nordiek
|
|

Nov 17 '05 #5
Christof,
| Yes, that could be (but does someone knows it for sure?).
You could always use Reflector or ILDASM to look at how the StringBuilder
class is implemented.

Looking back to your original question: Have you considered creating a
wrapper around StringBuilder that has the specific behavior you're after?

Unfortunately whether ToString itself makes a copy or any method after
ToString makes a copy, you are still going to have copies of the "string"
made. As the "string" needs to go from one StringBuilder to the other...

It sounds like you really need a StringBuilder.A ppend(StringBui lder) method
to have a single copy done instead of 2, however I don't see one of them...

Hope this helps
Jay
"Christof Nordiek" <cn@nospam.de > wrote in message
news:ur******** ******@tk2msftn gp13.phx.gbl...
| Hi Jay,
| thanks for responding.
|
| "Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> schrieb im
| Newsbeitrag news:eo******** ******@TK2MSFTN GP09.phx.gbl...
| > Christof,
| > My understanding is that StringBuilder.T oString does not copy the string
| > data, it just references the data as Johann stated.
| >
| > I understand that when you go to modify the StringBuilder data after
| > calling
| > it's ToString, it then copies the existing data & "starts over".
| > ....
| Yes, that could be (but does someone knows it for sure?).
| But that wouldn't solve my problem, since in most cases I will have to
reuse
| the
| original as StringBuilder, so the copy will occur anyway.
|
| > ....
| > "Christof Nordiek" <cn@nospam.de > wrote in message
| > news:Oj******** ********@TK2MSF TNGP14.phx.gbl. ..
| > | "Johann Blake" <jo*********@ya hoo.com> schrieb im Newsbeitrag
| > | news:11******** *************@g 14g2000cwa.goog legroups.com...
| > | > Christof,
| > | >
| > | > What you are doing is correct. The ToString() method does not copy
the
| > | > string data in the StringBuilder instance. It just references the
| > data.
| > | >
| > | > Best Regards
| > | > Johann Blake
| > | >
| > |
| > | Can't imagine that.
| > | If it was so, then the content of the string Instance could be changed
| > by
| > | the StringBuilder.
| > |
| > | Best Regards
| > | Christof Nordiek
| > |
| > |
| >
| >
|
|
Nov 17 '05 #6

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

Similar topics

79
14121
by: Klaus Bonadt | last post by:
In order to protect software from being copied without licence, I would like to use something like a key, which fits only to the current system. The serial number of the CPU or the current operating system would be appropriate. However, I do not know how to retrieve the information. Could you help? Klaus
1
21614
by: Waldemar FEM | last post by:
What is an easy way of making a deep copy of an XmlDocument object? Right now I'm doing something like this: XmlDocument inputDoc; XmlDocument copyDoc; ..... StringBuilder builder = new StringBuilder(); TextWriter textWriter = new StringWriter(builder); XmlTextWriter writer = new XmlTextWriter(textWriter); inputDoc.WriteTo(writer);
20
1899
by: Alvin Bruney | last post by:
On the advice of a user, I've timed stringbuilder v string. Here are the results. Here are the numbers: Total # queries 3747 Time in Milliseconds StringBuilder: String 460.6624 320.4608 350.504 220.3168
9
9159
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 StringBuilder. At present I am porting a VB6 webclass app to VB.NET and therefore I am trying to make it as efficent as possible via the new functionality of VB.NET.
2
2686
by: Keith | last post by:
I'm trying to come up with a way to create a contextmenu that will do all the "standard" functions (cut, copy, paste, undo, etc). There seems to be a lot of information out there - but nothing seems to work for me. A few people refer to txt.copy() txt.paste(), etc. I'm not sure if those are old functions, but they do not work for me. I've tried textbox1.selectall() - that works in selecting
12
4927
by: DumberThanSnot | last post by:
is there a faster way to copy an ArrayList of strings to a string other than a tight loop. In it's most simple terms, I'm currently using something like this... ---------------------------- *** NOTE: rtbXML is a Rich Text Box on the form dim iCntr as integer
3
12799
by: marfi95 | last post by:
Hi all. I need to copy a byte array into a string, but starting at a specific location in the byte array. This is where I get hung up. For example if my byte array is (100) big, I might want to start at position 60 for example and copy from 60 to the next null byte in the array to my string. The starting position is variable, as is where the next null byte is in the byte array. The array I'm dealing with is much bigger than that, so...
12
7220
by: CMirandaman | last post by:
Sounds like a stupid question I know. I can tell that they are used to copy strings. But what is the difference between x = y; versus x = String.Copy(y); Or are they essentially the same?
5
2806
by: redamazon200 | last post by:
I am looking for a way to copy a pattern (letter 'A' in the following example) to another string. string str1 = "1111AAAA111111AA"; string str2 = "1111000000001111"; After the copy str2 becomes "1111AAAA000011AA". Can this is done efficiently using Regex? TIA
0
9643
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10319
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
10147
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...
1
10087
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9947
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
8971
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...
0
6737
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
5380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.