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

Generating very large string in C#

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

Jun 12 '06 #1
16 26016
Use the StringBuilder class and its Append method.
<Du****@yahoo.com> wrote in message
news:11*********************@h76g2000cwa.googlegro ups.com...
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

Jun 13 '06 #2
Is this really elegant?

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

Thanks!

Scott M. wrote:
Use the StringBuilder class and its Append method.
<Du****@yahoo.com> wrote in message
news:11*********************@h76g2000cwa.googlegro ups.com...
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


Jun 13 '06 #3
ta*****@gmail.com wrote:
Is this really elegant?

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


This won't compile - for one, StringBuilder takes an Int32 argument, and
for another, it's UInt32.MaxValue. If you pass Int32.MaxValue, it should
work on a 64-bit system, although it might be slow if there isn't enough
memory.

The OP specified on the order of 1MB, so talk of such large cases as 4GB
(int.MaxValue * sizeof(char)) is fairly irrelevant.

If the task is generating a string in an output fashion only (i.e. no
random access, replacements, insertions etc. needed), where it has to
scale far beyond 1MB or so, writing to a file will be more efficient,
but then the string won't be in memory, and so the source code itself
that works with the string may be less elegant.

-- Barry

--
http://barrkel.blogspot.com/
Jun 13 '06 #4
>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?


string large = new string('?', 512*1024);

Should take roughly 1 MB.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jun 13 '06 #5
<Du****@yahoo.com> wrote:
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?


string x = new string ('x', 1024*1024/2);

(The division by two is to take account of each character being 2
bytes.)

--
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
Jun 13 '06 #6

"Barry Kelly" <ba***********@gmail.com> wrote in message
news:ce********************************@4ax.com...
| ta*****@gmail.com wrote:
|
| > Is this really elegant?
| >
| > What if I use directly StringBuilder str = new
| > StringBuilder(UInt32.Max)? or smaller capacity?
|
| This won't compile - for one, StringBuilder takes an Int32 argument, and
| for another, it's UInt32.MaxValue. If you pass Int32.MaxValue, it should
| work on a 64-bit system, although it might be slow if there isn't enough
| memory.
|

It won't work on a 64 bit system either, a string (like any other CLR
object) is limitted to 2GB irrespective the OS version.

Willy.


Jun 13 '06 #7
"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote:
It won't work on a 64 bit system either, a string (like any other CLR
object) is limitted to 2GB irrespective the OS version.


I have not yet had the luxury or necessity to test on a 64-bit sysem
yet... :)

-- Barry

--
http://barrkel.blogspot.com/
Jun 13 '06 #8

"Barry Kelly" <ba***********@gmail.com> wrote in message
news:h6********************************@4ax.com...
| "Willy Denoyette [MVP]" <wi*************@telenet.be> wrote:
|
| > It won't work on a 64 bit system either, a string (like any other CLR
| > object) is limitted to 2GB irrespective the OS version.
|
| I have not yet had the luxury or necessity to test on a 64-bit sysem
| yet... :)
|

If yo can't test it yourself, just read this snip:
<First some background; in the 2.0 version of the .Net runtime (CLR) we made
a conscious design decision to keep the maximum object size allowed in the
GC Heap at 2GB, even on the 64-bit version of the runtime.>

from: http://blogs.msdn.com/joshwil/archiv...10/450202.aspx

Willy.
Jun 13 '06 #9
"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote:
If yo can't test it yourself, just read this snip:


Yes. Now that you mentioned it, I do dimly recall this. However, I
usually test everything I post on the newsgroup here, so I would have
tested it anyway.

-- Barry

--
http://barrkel.blogspot.com/
Jun 13 '06 #10
Still not a good idea if the string is to be built over several steps,
rather than just one assignment. StringBuilder would be better.
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:ea**************@TK2MSFTNGP05.phx.gbl...
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?


string large = new string('?', 512*1024);

Should take roughly 1 MB.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Jun 13 '06 #11
Scott M. <s-***@nospam.nospam> wrote:
Still not a good idea if the string is to be built over several steps,
rather than just one assignment. StringBuilder would be better.


Why? I don't see anything in the OP's original post to indicate that
anything beyond a large string is required. Using that constructor is
the simplest way of generating such a string, IMO. In what way would
StringBuilder be better?

--
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
Jun 13 '06 #12
Willy Denoyette [MVP] wrote:
"Barry Kelly" <ba***********@gmail.com> wrote in message
news:h6********************************@4ax.com...
| "Willy Denoyette [MVP]" <wi*************@telenet.be> wrote:
|
| > It won't work on a 64 bit system either, a string (like any other CLR
| > object) is limitted to 2GB irrespective the OS version.
|
| I have not yet had the luxury or necessity to test on a 64-bit sysem
| yet... :)
|

If yo can't test it yourself, just read this snip:
<First some background; in the 2.0 version of the .Net runtime (CLR) we made
a conscious design decision to keep the maximum object size allowed in the
GC Heap at 2GB, even on the 64-bit version of the runtime.>

from: http://blogs.msdn.com/joshwil/archiv...10/450202.aspx

Willy.


Another "who would ever need more than 640 kb" decision.... ;)
Jun 13 '06 #13
As stated, a StringBuilder would be better *if* the string is to be built up
over several expressions.
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Scott M. <s-***@nospam.nospam> wrote:
Still not a good idea if the string is to be built over several steps,
rather than just one assignment. StringBuilder would be better.


Why? I don't see anything in the OP's original post to indicate that
anything beyond a large string is required. Using that constructor is
the simplest way of generating such a string, IMO. In what way would
StringBuilder be better?

--
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

Jun 14 '06 #14
Scott M. <s-***@nospam.nospam> wrote:
As stated, a StringBuilder would be better *if* the string is to be built up
over several expressions.


Better than a concatenation, yes. I don't think anyone was suggesting
that though.

--
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
Jun 14 '06 #15
Jon Skeet [C# MVP] wrote:
Scott M. <s-***@nospam.nospam> wrote:
As stated, a StringBuilder would be better *if* the string is to be built up
over several expressions.


Better than a concatenation, yes. I don't think anyone was suggesting
that though.


No, the only suggested alternative was using a string constructor that
creates a huge string filled with the same character.

That fulfills the only requirements that the OP had, e.g. that the
string should be large, and that the way to create it should be elegant.

If that solution really is sufficient for the test that the OP is going
to do depends on information about the test that was not revealed in the
original post.

Perhaps time (e.g. the OP) will tell... ;)
Jun 14 '06 #16

"Göran Andersson" <gu***@guffa.com> wrote in message
news:OF******************@TK2MSFTNGP05.phx.gbl...
| Willy Denoyette [MVP] wrote:
| > "Barry Kelly" <ba***********@gmail.com> wrote in message
| > news:h6********************************@4ax.com...
| > | "Willy Denoyette [MVP]" <wi*************@telenet.be> wrote:
| > |
| > | > It won't work on a 64 bit system either, a string (like any other
CLR
| > | > object) is limitted to 2GB irrespective the OS version.
| > |
| > | I have not yet had the luxury or necessity to test on a 64-bit sysem
| > | yet... :)
| > |
| >
| > If yo can't test it yourself, just read this snip:
| > <First some background; in the 2.0 version of the .Net runtime (CLR) we
made
| > a conscious design decision to keep the maximum object size allowed in
the
| > GC Heap at 2GB, even on the 64-bit version of the runtime.>
| >
| > from: http://blogs.msdn.com/joshwil/archiv...10/450202.aspx
| >
| > Willy.
| >
|
| Another "who would ever need more than 640 kb" decision.... ;)

I don't see the link, anyway, remember reference type instances are
'moveable' unless they are larger than 85Kb, that means that strings larger
than 85Kb are stored on the large object heap which never gets compacted,
simply because it's too costly to move such large blocks of memory (ever
thought what it would take to move 2GB objects in memory).
Note also that .NET is not the silver bullet, if you really need such large
contigious strings, you'll have to allocate them from the process heap using
unmanaged code.

Willy.

Jun 14 '06 #17

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

Similar topics

7
by: el_roachmeister | last post by:
When I run these two lines from my SSH term $out is printed fine to STDOUT. When I run it from a web browser nothing is printed out? I have many php cgi scripts, so I am stumbled as to why this one...
3
by: Bob Maggio | last post by:
I have created a function that returns a string containing raw HTML to be used on a web form (see below): private string GetReportHTML() { string strHTML = ""; // I have some code here that...
11
by: Gnik | last post by:
I created a C# application with some large string constants and whenever I try to build the solution the compiler crashes with no error message. On further analysis the problem seems to occur when...
15
by: Daren | last post by:
Hi, I need to be able to split large string variables into an array of lines, each line can be no longer than 70 chars. The string variables are text, so I would additionally like the lines...
3
by: John | last post by:
Hi I need to generate a string of six random characters to act as the first password. Is there an example of how to do this somewhere? Thanks Regards
3
by: B.r.K.o.N.j.A | last post by:
example code function myfunc1() { return myfunc2(); } function myfunc2() { return myfunc3();
4
by: Terry Olsen | last post by:
I'm reading the response to the DOS "FIND" command into a string. Until now it has worked just fine. Now it's returning so much data that the string seems to be full. Here's a snippet of the last...
0
by: phaniva | last post by:
Hi, I have a ASP .Net 2.0 web service on Win 2k SP4. One of the web methods expects a string as input like public string mymethod(string inp); I am trying to invoke this web method through...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.