在C#中生成非常大的字符串 [英] Generating very large string in C#

查看:80
本文介绍了在C#中生成非常大的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨伙计,


我需要在我的C#代码中生成一个非常大的字符串(1 MB左右),所以我

可以测试代码。


最优雅的方式是什么?


谢谢!


Dim

Hi Folks,

I need to generate a very large string (1 MB or so) in my C# code, so I
can test the code.

What is the most elegant way to do so?

Thanks!

Dim

推荐答案

使用StringBuilder类及其Append方法。

< Du **** @ yahoo.com>在消息中写道

新闻:11 ********************* @ h76g2000cwa.googlegro ups.com ...
Use the StringBuilder class and its Append method.
<Du****@yahoo.com> wrote in message
news:11*********************@h76g2000cwa.googlegro ups.com...
嗨伙计,

我需要在我的C#代码中生成一个非常大的字符串(大约1 MB),所以我可以测试代码。

最优雅的方式是什么?

谢谢!

昏暗
Hi Folks,

I need to generate a very large string (1 MB or so) in my C# code, so I
can test the code.

What is the most elegant way to do so?

Thanks!

Dim



这真的很优雅吗?


如果我直接使用StringBuilder str = new

StringBuilder(UInt32.Max)怎么办?或者更小的容量?


谢谢!


Scott M.写道:
Is this really elegant?

What if I use directly StringBuilder str = new
StringBuilder(UInt32.Max)? or smaller capacity?

Thanks!

Scott M. wrote:
使用StringBuilder类及其附加方法。

< Du **** @ yahoo.com>在消息中写道
新闻:11 ********************* @ h76g2000cwa.googlegro ups.com ...
Use the StringBuilder class and its Append method.
<Du****@yahoo.com> wrote in message
news:11*********************@h76g2000cwa.googlegro ups.com...
你好伙计,

我需要在我的C#代码中生成一个非常大的字符串(大约1 MB),所以我可以测试代码。

什么是最优雅的方式呢?

谢谢!

Dim
Hi Folks,

I need to generate a very large string (1 MB or so) in my C# code, so I
can test the code.

What is the most elegant way to do so?

Thanks!

Dim





ta*****@gmail.com 写道:
ta*****@gmail.com wrote:
这真的很优雅吗?

如果我直接使用StringBuilder str = new
StringBuilder(UInt32.Max)怎么办?或更小的容量?
Is this really elegant?

What if I use directly StringBuilder str = new
StringBuilder(UInt32.Max)? or smaller capacity?




这不会编译 - 一个,StringBuilder接受一个I​​nt32参数,而

换另一个,它'是UInt32.MaxValue。如果您传递Int32.MaxValue,它应该在64位系统上工作,但如果没有足够的内存,那么它可能会很慢。


OP大约1MB的指定,所以谈论像4GB这样的大型案例
(int.MaxValue * sizeof(char))是相当无关紧要的。 />

如果任务只以输出方式生成一个字符串(即没有

随机访问,需要替换,插入等),它必须在哪里/>
规模远远超过1MB左右,写入文件将更有效,

然后字符串不会在内存中,所以源代码本身

与字符串一起使用可能不太优雅。


- Barry


-
< a rel =nofollowhref =http://barrkel.blogspot.com/target =_ blank> http://barrkel.blogspot.com/


这篇关于在C#中生成非常大的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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