473,614 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 64804
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.A ppend
On Thu, 14 Sep 2006 12:31:56 +0200, Martin Pöpping
<ma******@despa mmed.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******@despa mmed.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.ukwr ote in message
news:0_******** ************@ec lipse.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******@despa mmed.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******** ********@TK2MSF TNGP02.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******** *******@TK2MSFT NGP06.phx.gbl.. .
| "Greg Young" <dr************ *******@hotmail .comwrote in message
| news:%2******** ********@TK2MSF TNGP02.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
1702
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 col, right? Or am I completely lost? Cheers,
12
2194
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 add an extra password onto the end of the line and it does not work. I assumed it might be 255 characters, but the line that works is much longer than that. dixie
5
11729
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 MyString As String Am I getting VLS declaration or a FLS declaration? Can I control which I get somehow? I have done some homework, but I don't understand
1
9869
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 result of the formatting should be a string no longer than, say, 3 characters? I know I can easily specify the minimum length, but I am producing a report with columns. I need to
3
22023
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 the id string. ? tia. -- http://www.ren-prod-inc.com/hug_soft/store.php?action=contact
2
3385
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 bad programming techniques. TIA Paul
2
2902
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 string that exceeds a certain fixed length. For this application if the string parameter exceeds the specified length it will always be an error, so I would like to put that rule explicitly in the contract rather than raise the error once I get...
2
5325
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
1666
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: gridviews has one column with three names Ram Murali Hari Krishna Murthy Sharma
0
8182
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8130
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8579
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8279
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8433
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5540
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4127
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1747
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1425
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.