473,242 Members | 1,581 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,242 software developers and data experts.

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 7133
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 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 #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 Löwendahl [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 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 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 Löwendahl [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
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.