473,473 Members | 1,984 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

What is the purpose of the string.copy method?

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?
Jan 21 '06 #1
12 7152
CMirandaman <CM*********@discussions.microsoft.com> wrote:
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?


String.Copy returns a new string with the same content. Assignment uses
the same *reference*.

One potential use of it is to "trim" strings which have been
constructed with StringBuilder. They often have "padding" at the end of
the string (to allow for appending while they're still in
StringBuilder). I've used the equivalent of this in Java a few times -
can't say I've ever used it in .NET.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 21 '06 #2
Hello CMirandaman,

Essentially the first one ( x = y ) do not copy the string, it just assigns
x the same reference that y has. Meaning it get's a reference to the string
object, not a copy.
While string.Copy makes a real copy of the string.

--
Patrik Lwendahl [C# MVP]
http://www.lowendahl.net
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?

Jan 21 '06 #3
Sorry to be dense about this but is there a practical difference as far as
writing code is concerned? If I run the following code:
string s;
string p;
s = "Hello";
p = s;
s = "Goodbye";
The output is "Hello" so it appears that assignment functions like a copy..
Can you give me an example of when the Copy method would be needed?

"Patrik Löwendahl [C# MVP]" wrote:
Hello CMirandaman,

Essentially the first one ( x = y ) do not copy the string, it just assigns
x the same reference that y has. Meaning it get's a reference to the string
object, not a copy.
While string.Copy makes a real copy of the string.

--
Patrik Löwendahl [C# MVP]
http://www.lowendahl.net
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?


Jan 21 '06 #4
On Sat, 21 Jan 2006 00:05:02 -0800, "CMirandaman"
<CM*********@discussions.microsoft.com> wrote:
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?


With x = y you get two pointers pointing to the same piece of memory,
remember that strings are reference types:

x-------> "abcdef"
^
|
y----------|
With x = String.Copy(y) you get two different copies of the string
itself:

x-------> "abcdef"
y-------> "abcdef"
rossum

--

The ultimate truth is that there is no ultimate truth
Jan 21 '06 #5
I think the OP has a point since strings are immutable, interned and
automatically newed. If both references refer to strings with the same
content, they may actually refer to the same interned string.

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Jan 22 '06 #6
No, they don't. Strings are interned only at compile time. At run time
it is perfectly possible (and common) to have two separate strings with
the same contents. (Kudos to Jon Skeet who corrected me on this until
it sunk in.)

Jon answered the OP's question: String.Copy is useful in some very
obscure circumstances having to do with StringBuilder.

I've never used it myself.

Jan 22 '06 #7
Hello.

well, you let S point to a new string object but P still points to the original
one so that's not strange; what you do is just playing with references. And
you are correct in that if you concatinate S instead, like so:

s = "hello";
p = s;
s += " world"

the output for p would be "hello" and for s "hello world" since a new object
is created (strings are immutable) and thus we wouldn't need copying. I can't
give you an practical example on when string.Copy would be needed since I
after 4 years yet have to find one. But that is the technical difference
anyhow.

--
Patrik Lwendahl [C# MVP]
http://www.lowendahl.net
Sorry to be dense about this but is there a practical difference as
far as
writing code is concerned? If I run the following code:
string s;
string p;
s = "Hello";
p = s;
s = "Goodbye";
The output is "Hello" so it appears that assignment functions like a
copy..
Can you give me an example of when the Copy method would be needed?

"Patrik Lwendahl [C# MVP]" wrote:
Hello CMirandaman,

Essentially the first one ( x = y ) do not copy the string, it just
assigns
x the same reference that y has. Meaning it get's a reference to the
string
object, not a copy.
While string.Copy makes a real copy of the string.
--
Patrik Lwendahl [C# MVP]
http://www.lowendahl.net
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?

Jan 22 '06 #8
Looking at Jon's page I see that it is only string literals that are
interned and for
practical purposes strings _act_ as a value type. So I still think the
OP has a valid
point as I have never needed to use String.Copy.

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Jan 22 '06 #9
Hello Jeff,

I do agree. string.Copy has no real value since the CLR is doing a lot of
work under the covers.

Noteable though is that you have the option to intern you're own strings.

--
Patrik Lwendahl [C# MVP]
http://www.lowendahl.net
Looking at Jon's page I see that it is only string literals that are
interned and for
practical purposes strings _act_ as a value type. So I still think the
OP has a valid
point as I have never needed to use String.Copy.
Regards,
Jeff
*** Sent via Developersdex http://www.developersdex.com ***

Jan 22 '06 #10
A bit artificial but:

class X
{
string s;

public X(string ss) { s = String.Copy(s); }

public F()
{
lock(s) { .... }
}
}

using the supplied string instead of a copy could cause problems

Jan 22 '06 #11
Nick Hounsome <nh***@nickhounsome.me.uk> wrote:
A bit artificial but:

class X
{
string s;

public X(string ss) { s = String.Copy(s); }

public F()
{
lock(s) { .... }
}
}

using the supplied string instead of a copy could cause problems


Yup. I saw some code just the other day which was locking on a string.
Of course, that's a really bad idea in itself IMO, but there we go...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 22 '06 #12
Many thanks for all the replies to my post. You helped clarify an issue for
me that I just couldn't find spelled out in any documentation.

"CMirandaman" wrote:
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?

Jan 23 '06 #13

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

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
5
by: Sid | last post by:
Hi, I was going through this reference code and playing around with it. The code (shown below) shows a class that represents a stack of string pointers In the code below in particular in the...
7
by: SIgnOff | last post by:
What the fastest way to copy strings from file to a vector in STL?
6
by: chris | last post by:
Hi, I have a table in the database. I have a method which will return a table with the same structure as the DB one and with all data I need. What I want to do is to remove all data in the DB...
26
by: Lasse Edsvik | last post by:
Hello I'm trying to build a simple COM+ app in vs.net using C# and i cant register it in component manager..... what more is needed than this: using System; using...
6
by: brian_harris | last post by:
I have a function that passes a string class pointer to a function, this function is then suppose to fill it and the outer function uses it. But I believe that I am running into problem due to...
17
by: baibaichen | last post by:
i have written some code to verify how to disable slicing copy according C++ Gotchas item 30 the follow is my class hierarchy, and note that B is abstract class!! class B { public: explicit...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
7
by: fl | last post by:
Hi, I am learning C++ with C++ primer written by Lippman. The first output line: cout << s1 << endl; works right. The second is wrong. I find the problem is that s1 is destroyed in the call from...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
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...
1
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...
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...
1
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...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.