关于字符串Class和StringBuilder类 [英] about the string Class and the StringBuilder class

查看:69
本文介绍了关于字符串Class和StringBuilder类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!


我在一本书中读到这里是一个问题,答案是我不满足于b $ b满意。

什么时候应该使用StringBuilder类而不是String类。

1.用较短的字符串构建字符串。

2.使用文本时数据超过256个字节。

3.当你想搜索并替换字符串的内容时

4.当字符串是值类型时。


我选择的答案是3,因为字符串对象是不可变的

所以

这里使用StringBuilder类是有利的但是

根据这本书是正确的答案1.


但是如果你用较短的字符串构建一个字符串你就不必使用

StringBuilder类为

string s =" This" +是 +a +测试;


我可以接受数字1,如果他们的意思是这个。

string s;

s + =" ;这个;

s + ="是;

s + =" a" ;;

s + =" Test";


但是当他们说用较短的琴弦构建一个字符串

时,有些不清楚它们是什么意思。


可以有人评论这本书怎么说正确的答案是

数字1


// Tony

推荐答案

9月22日,12:25 * pm,Tony Johansson < johansson.anders ... @ telia.com>

写道:
On Sep 22, 12:25*pm, "Tony Johansson" <johansson.anders...@telia.com>
wrote:

你好!


我读了一本书,这是一个问题和答案,我不是很满意。

什么时候应该使用StringBuilder类而不是String类。

1.从较短的字符串构建字符串时

2.使用长度超过256字节的文本数据时

3.当你想搜索和替换字符串的内容时

4.字符串是值类型。


我会选择的答案将是3因为字符串对象是不可变的

所以

这里使用StringBuilder类是一个优点,但

根据本书是正确答案1.


但是如果你用较短的字符串构建一个字符串,你就不必使用

的StringBuilder类作为

string s =" This" +是 +a +测试;


我可以接受数字1,如果他们的意思是这个。

string s;

s + =" ;这个;

s + ="是;

s + =" a" ;;

s + =" Test";


但是当他们说用较短的琴弦构建一个字符串

时,有些不清楚它们是什么意思。


可以有人评论这本书怎么说正确的答案是

数字1


// Tony
Hello!

I read in a book and here is a question and the answer that I''m not
satisfied with.
When should you use the StringBuilder class instead of the String class.
1.When building a string from shorter strings.
2.When working with text data longer than 256 bytes.
3.When you want to search and replace the contents of a string
4.When a string is a value type.

The answer that I would choose would be 3 because string object is immutable
so
here is it an advantage to use the StringBuilder class but
according to the book is the right answer 1.

But if you build a string from shorter strings you don''t have to use a
StringBuilder class as
string s = "This" + "is" + "a" + "Test";

I can accept numer 1 if they mean this.
string s;
s += "This";
s += "is";
s += "a";
s += "Test";

But it is somewhat unclear what they mean when they say building a string
from shorter strings.

Can somebody give a comment how the book can say that the right answer is
number 1

//Tony



这本书一定是指连词。 StringBuilder

在这种情况下会更有效率,因为它使用需要更少复制操作的技术

。请参阅 http://pobox.com/~skeet/csharp/stringbuilder.html

了解更多信息。

The book must have been referring to concatentation. StringBuilder
will be more efficient in that scenario because it uses techniques
that require fewer copy operations. See http://pobox.com/~skeet/csharp/stringbuilder.html
for more information.


9月22日,1:13 * pm,Brian Gideon< briangid ... @ yahoo.comwrote:
On Sep 22, 1:13*pm, Brian Gideon <briangid...@yahoo.comwrote:

9月22日,12:25 * pm,Tony Johansson < johansson.anders ... @ telia.com>

写道:


On Sep 22, 12:25*pm, "Tony Johansson" <johansson.anders...@telia.com>
wrote:



您好!
Hello!


我读了一本书,这是一个问题和答案,我不是很满意。

什么时候应该使用StringBuilder类而不是String类..

1.用较短的字符串构建字符串。

2。使用长度超过256字节的文本数据时。

3.当您想要搜索并替换字符串的内容时

4.当字符串是值类型时。
I read in a book and here is a question and the answer that I''m not
satisfied with.
When should you use the StringBuilder class instead of the String class..
1.When building a string from shorter strings.
2.When working with text data longer than 256 bytes.
3.When you want to search and replace the contents of a string
4.When a string is a value type.


我选择的答案是3,因为字符串对象是不可变的

所以

这里使用StringBuilder类是有利的,但

根据本书是正确答案1.
The answer that I would choose would be 3 because string object is immutable
so
here is it an advantage to use the StringBuilder class but
according to the book is the right answer 1.


但是如果你用较短的字符串构建一个字符串,你不必使用

StringBuilder类作为

string s =" This" +是 +a +测试;
But if you build a string from shorter strings you don''t have to use a
StringBuilder class as
string s = "This" + "is" + "a" + "Test";


我可以接受数字1,如果他们的意思是这个。

string s;

s + =" This;

s + =" is;

s + =" a" ;;

s + =" Test" ;
I can accept numer 1 if they mean this.
string s;
s += "This";
s += "is";
s += "a";
s += "Test";


但是当他们说用较短的字符串构建一个字符串

时,有些不清楚它们是什么意思。
But it is somewhat unclear what they mean when they say building a string
from shorter strings.


有人可以评论这本书怎么说正确的答案是

number 1
Can somebody give a comment how the book can say that the right answer is
number 1


// Tony
//Tony



这本书一定是指连词。 * StringBuilder

在这种情况下效率会更高,因为它使用的技术需要更少的复制操作。 * Seehttp://pobox.com/~skeet/csharp/stringbuilder.html

了解更多信息。


The book must have been referring to concatentation. *StringBuilder
will be more efficient in that scenario because it uses techniques
that require fewer copy operations. *Seehttp://pobox.com/~skeet/csharp/stringbuilder.html
for more information.



哦,顺便说一句,我应该指出你的例子在

中是唯一的,在编译时串联会发生个人的时间

字符串是不变的。在这方面,StringBuilder不会更好地执行

Oh, by the way, I should point out that you''re example is unique in
that the concatenation will occur at compile time since the individual
strings are constant. In that respect the StringBuilder would not
perform any better.


9月22日下午1:25,Tony Johansson < johansson.anders ... @ telia.com>

写道:
On Sep 22, 1:25 pm, "Tony Johansson" <johansson.anders...@telia.com>
wrote:

你好!


我读了一本书,这是一个问题和答案,我不是很满意。

什么时候应该使用StringBuilder类而不是String类。

1.从较短的字符串构建字符串时

2.使用长度超过256字节的文本数据时

3.当你想搜索和替换字符串的内容时

4.字符串是值类型。


我会选择的答案将是3因为字符串对象是不可变的

所以

这里使用StringBuilder类是一个优点,但

根据本书是正确答案1.


但是如果你用较短的字符串构建一个字符串,你就不必使用

的StringBuilder类作为

string s =" This" +是 +a +测试;


我可以接受数字1,如果他们的意思是这个。

string s;

s + =" ;这个;

s + ="是;

s + =" a" ;;

s + =" Test";


但是当他们说用较短的琴弦构建一个字符串

时,有些不清楚它们是什么意思。


可以有人评论这本书怎么说正确答案是

数字1
Hello!

I read in a book and here is a question and the answer that I''m not
satisfied with.
When should you use the StringBuilder class instead of the String class.
1.When building a string from shorter strings.
2.When working with text data longer than 256 bytes.
3.When you want to search and replace the contents of a string
4.When a string is a value type.

The answer that I would choose would be 3 because string object is immutable
so
here is it an advantage to use the StringBuilder class but
according to the book is the right answer 1.

But if you build a string from shorter strings you don''t have to use a
StringBuilder class as
string s = "This" + "is" + "a" + "Test";

I can accept numer 1 if they mean this.
string s;
s += "This";
s += "is";
s += "a";
s += "Test";

But it is somewhat unclear what they mean when they say building a string
from shorter strings.

Can somebody give a comment how the book can say that the right answer is
number 1



最好的答案是1.

我认为你会更好地理解为什么不是3.

例如。如果你想替换字符串a,对于A,所有你需要做的就是调用string.Replace(这可以保证你会使用

最合适的算法)。


您提供的连接示例并不是最好的。

编译器将仅在一个@编译时间内连接所有字符串。

但是这个想法仍然是每个连接将生成一个字符串。

所以你结束了很多没有用的临时工具。那个

为什么最好使用StrngBuilder

The best answer is the 1.
I think that you will understand better why not is 3.
For example. if you want to replace the string "a" for "A" all you
have to do is call string.Replace (which assure you it will use the
most appropriated algorithm).

The example you provide of contatenation is not the best one. The
compiler will contatenate all the strings in only one @ compile time.
But the idea remains that each concatenation will generate one string.
so you end with a lot of temporaries strnigs that have no use. That
why is better to use StrngBuilder


这篇关于关于字符串Class和StringBuilder类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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