how long can a string be

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm receiving info from a com port into a string. I gradually process the
string which constantly shortens it. The question is how long can a string be
before I need to write some info to disk inorder not to loose any information?
 
Hi Dave,

As far as I know the only limitations is your system's available memory, which for 32-bit systems is limited to 4GB.
 
I do not know how long can a particular string be, but I would suggest using
StringBuilder in case you need to manipulate string data very often, because
string would make huge overhead

Dave said:
I'm receiving info from a com port into a string. I gradually process the
string which constantly shortens it. The question is how long can a string be
before I need to write some info to disk inorder not to loose any
information?
 
I don't see a limit in the documentation, however String.Length returns an
int so one could infer that a string cannot be greater than Int32.MaxValue in
length -- over 2 billion characters.

If you're processing the string that much you may want to consider using a
StringBuilder vs. String - it would probably be much more efficient.

KH
 
Thanks, that answers my question.

Second question, I need to be able to find a certain character in the
string. I use InderOf to fine this character in a string. Is there an
equivalent command in StringBuilder. If I have to convert the StringBuilder
to a string first
before I can find this character, then that would defeat the purpose of using
StringBuilder, because I wouldn't know how much of the StringBuilder to
convert to a string without knowing where the character is.
 
Check the .NET SDK - look up "StringBuilder" in the index

So I did that for you and StringBuilder does not have an IndexOf method.

However, you can look for a char by iterating all chars in the
StringBuilder, which is exactly what String.IndexOf does anyway:

StringBuilder sb = new StringBuilder("foo");
foreach (char ch in sb);

In any case you're probably going to want to be using StringBuilder because
it is mutable whereas String it not -- every action you perform on a string
returns a new string; it does not act on the same instance. So performing
many operations on what seems to be one string actually causes many instances
of string to be created.
 
Back
Top