string.copy方法的目的是什么? [英] What is the purpose of the string.copy method?

查看:78
本文介绍了string.copy方法的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

听起来像我知道的一个愚蠢的问题。我可以说他们习惯于复制

字符串。但是

x = y;

与x = String.Copy(y)之间有什么区别;


或者他们是基本相同?

解决方案

CMirandaman< CM ********* @ discussion.microsoft.com>写道:

听起来像我知道的一个愚蠢的问题。我可以说他们习惯于复制字符串。但是
x = y;
与x = String.Copy(y)之间有什么区别;

或者它们本质上是一样的吗?



String.Copy返回一个内容相同的新字符串。作业使用

相同*参考*。


一个潜在的用途是修剪用StringBuilder构造的字符串是

。它们通常具有填充。在

结尾处的字符串(允许在他们还在的时候附加

StringBuilder)。我已经在Java中使用了相当于这几次 -

不能说我曾经在.NET中使用它。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet

如果回复群组,请不要给我发邮件


你好CMirandaman,


基本上第一个(x = y)不复制字符串,它只分配

x与y相同的参考。意思是它是对字符串

对象的引用,而不是副本。

而string.Copy是字符串的真实副本。


-

Patrik L?wendahl [C#MVP]
http://www.lowendahl.net

听起来像我知道的一个愚蠢的问题。我可以说它们习惯于复制
字符串。但是
x = y;
与x = String.Copy(y)之间有什么区别;
或者它们基本相同?



很抱歉这个问题很密集,但就编写代码而言是否存在实际差异?

如果我运行以下代码:

string s;

string p;

s =" Hello";

p = s;

s =" Goodbye";

输出为Hello因此看起来分配函数就像一个副本..

你能给我一个关于何时需要复制方法的例子吗?


" Patrik L ?? wendahl [C#MVP]"写道:

你好CMirandaman,

基本上第一个(x = y)不复制字符串,它只是分配
x相同你有的参考。意思是它是对字符串
对象的引用,而不是副本。
而string.Copy是字符串的真实副本。

-
Patrik L ?? wendahl [C#MVP]
http://www.lowendahl.net

听起来像我知道的一个愚蠢的问题。我可以说它们习惯于复制
字符串。但是
x = y;
与x = String.Copy(y)之间有什么区别;
或者它们基本相同?




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?

解决方案

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


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?



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?




这篇关于string.copy方法的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆