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

Maximum length of a String?

Hello,

does a String in C# have a maximum length?

I tried to write a ToString Method of my class containing a hashtable.
At the beginning of the method i defined a String "ret".

In every iteration of my hashtable I add the content with ret += "...".

After some time my programm crashes.

If i use Console.Writeln in every iteration instead of ret += ...,
then everything works fine.

So I guess it has something to do with a possible maximum length of a
string?
Regards,

Martin
Sep 14 '06 #1
7 64718
Hi Martin,

The string itself should handle lengths well above your future memory
limits, but massive string concatenations won't work well if the hashtable
is large.

Instead of ret += "..." use StringBuilder.Append
On Thu, 14 Sep 2006 12:31:56 +0200, Martin Pöpping
<ma******@despammed.comwrote:
Hello,

does a String in C# have a maximum length?

I tried to write a ToString Method of my class containing a hashtable.
At the beginning of the method i defined a String "ret".

In every iteration of my hashtable I add the content with ret += "...".

After some time my programm crashes.

If i use Console.Writeln in every iteration instead of ret += ...,
then everything works fine.

So I guess it has something to do with a possible maximum length of a
string?
Regards,

Martin


--
Happy Coding!
Morten Wennevik [C# MVP]
Sep 14 '06 #2
String length is an int, which has a max value of (2,147,483,647)
characters. Your more likely to be hitting memory problems than string
length problems. Instead of doing += use a stringbuilder.

http://msdn.microsoft.com/library/de...ilderclass.asp

--
Regards

John Timney (MVP)
Visit my website:
http://www.johntimney.com
"Martin Pöpping" <ma******@despammed.comwrote in message
news:ee**********@newsreader2.netcologne.de...
Hello,

does a String in C# have a maximum length?

I tried to write a ToString Method of my class containing a hashtable.
At the beginning of the method i defined a String "ret".

In every iteration of my hashtable I add the content with ret += "...".

After some time my programm crashes.

If i use Console.Writeln in every iteration instead of ret += ...,
then everything works fine.

So I guess it has something to do with a possible maximum length of a
string?
Regards,

Martin

Sep 14 '06 #3
John Timney (MVP) schrieb:
String length is an int, which has a max value of (2,147,483,647)
characters. Your more likely to be hitting memory problems than string
length problems. Instead of doing += use a stringbuilder.

http://msdn.microsoft.com/library/de...ilderclass.asp
Thanks to all!

It works fine now.
Regards,

Martin
Sep 14 '06 #4
Martin Pöpping wrote:

<snip>
Thanks to all!

It works fine now.
For more info on where/why to use StringBuilder, see
http://www.pobox.com/~skeet/csharp/stringbuilder.html

Jon

Sep 14 '06 #5
John ...

If I remember correctly A few of the upper bits of the length (I believe 3
but don't quote me) are used as flags for other information .. so I believe
this limits it in the MS implementation to 28 bits in length which is
substantially shorter ... of course you are quite unlikely to hit this as
the limit will tend to be the largest contiguous area of memory available.

Cheers,

Greg

"John Timney (MVP)" <x_****@timney.eclipse.co.ukwrote in message
news:0_********************@eclipse.net.uk...
String length is an int, which has a max value of (2,147,483,647)
characters. Your more likely to be hitting memory problems than string
length problems. Instead of doing += use a stringbuilder.

http://msdn.microsoft.com/library/de...ilderclass.asp

--
Regards

John Timney (MVP)
Visit my website:
http://www.johntimney.com
"Martin Pöpping" <ma******@despammed.comwrote in message
news:ee**********@newsreader2.netcologne.de...
>Hello,

does a String in C# have a maximum length?

I tried to write a ToString Method of my class containing a hashtable.
At the beginning of the method i defined a String "ret".

In every iteration of my hashtable I add the content with ret += "...".

After some time my programm crashes.

If i use Console.Writeln in every iteration instead of ret += ...,
then everything works fine.

So I guess it has something to do with a possible maximum length of a
string?
Regards,

Martin


Sep 14 '06 #6
"Greg Young" <dr*******************@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
John ...

If I remember correctly A few of the upper bits of the length (I believe 3
but don't quote me) are used as flags for other information .. so I
believe this limits it in the MS implementation to 28 bits in length which
is substantially shorter ... of course you are quite unlikely to hit this
as the limit will tend to be the largest contiguous area of memory
available.
Won't virtual memory let you have a string that is larger than the largest
contiguous area of memory available.
Sep 14 '06 #7

"Allan Ebdrup" <eb****@noemail.noemailwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl...
| "Greg Young" <dr*******************@hotmail.comwrote in message
| news:%2****************@TK2MSFTNGP02.phx.gbl...
| John ...
| >
| If I remember correctly A few of the upper bits of the length (I believe
3
| but don't quote me) are used as flags for other information .. so I
| believe this limits it in the MS implementation to 28 bits in length
which
| is substantially shorter ... of course you are quite unlikely to hit
this
| as the limit will tend to be the largest contiguous area of memory
| available.
|
| Won't virtual memory let you have a string that is larger than the largest
| contiguous area of memory available.
|
|

No, the current CLR versions (both 32 and 64bit) limits the object size to a
maximum of 2GB. As, a System.String instance is just an array of unicode
bytes, it means that the string is limited to 1GB characters. Note that on a
32 OS, you will never be able to allocate that object size from the heap,
here the max. size of a string will be much less tha 1GB.

Willy.


Sep 14 '06 #8

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

Similar topics

0
by: Tobias | last post by:
Hi all, am I right assuming that the maximum length for a varchar field I can fetch with the sybase_fetch_* functions is 255? This maximum can only be circumvented by using a text or image...
12
by: dixie | last post by:
Is there a maximum length in VBA of a line containing stuff like the following: If password = "1234" or password = "1235" or password = ............... Then It seems to me that at some stage, I...
5
by: MLH | last post by:
I'm working with lots of long strings now, it seems. I have to import them & parse them constantly. The A97 memo field type supports only 32768 chars. What happens when this is processed... Dim...
1
by: Alan Foxmore | last post by:
Hello all, Is it possible to use String.Format() to specify a maximum length for a formatted item? For example, let's say I have: String.Format("{0}", "FREDDY"); How can I specify that the...
3
by: hug | last post by:
The valid character set for the ID= attribute of html tags is listed here (http://www.htmlhelp.com/reference/html40/attrs.html) but I am having difficulty finding the maximum allowable length of...
2
by: Paul Johnston | last post by:
I know this may sound silly but given something like if ($_ =~m/(one)|(two)|(three)/) Is there a maximum length on the size of the item between "/" and" /" ? Just curious as regards showing...
2
by: =?Utf-8?B?Z3Jva25yb2xs?= | last post by:
Is there a way that I can define a WebMethod with a parameter that is a fixed length string? I'm using VB.Net 2005 and would like to define a webmethod that will prevent the caller from passing a...
2
by: karthee | last post by:
all, what is the maximum length(text) of a node in a treeview. can we set the text length for a treenode? -Karthee
0
by: Ramu528 | last post by:
Friends, I am working on excel sheet. i populated gridview with excel sheet data. the gridview has one column. have to find out the maximum length of String in that column. For example:...
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
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...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.