how long can a string be

I have a pretty large text file, with very long lines. I am trying to read each line, match a particular field in that line (this is a comma deliminated text file) and then insert the coresponding field. I have done this and it works on most of the files. The problem is with the file that contains very long lines. It just screws everything up. It seems to either be having a problem reading the line as a string, or writing it back to a file. How long can a string be Is there some other method besides streamwriter/reader that can deal with very large lines of text Thanks.

Answer this question

how long can a string be

  • olidem

    >> How long can a string be

    Internally, System.String object uses Int32 for its length (m_stringLength or String.Length), hence, we can safely assume that the maximum number of characters a System.String can hold is within the Int32's limit (Int32.Max).

    >> Is there some other method besides streamwriter/reader that can deal with very
    >> large lines of text


    Yes. Try the System.Text.StringBuilder class, it is built for cases like what you described.

    Regards,

    -chris


  • hafe

    Thanks for the correction Jon.

    Regards,

    -chris

  • rrlinton

    How long is a piece of string

    You can use the Read method to read a certain number of characters at a time...

                using (StreamReader sr = new StreamReader(path))
                {
                    //This is an arbitrary size for this example.
                    char[] c = null;

                    while (sr.Peek() >= 0)
                    {
                        c = new char[5];
                        sr.Read(c, 0, c.Length);
                        //The output will look odd, because
                        //only five characters are read at a time.
                        Console.WriteLine(c);
                    }
                }

    ...but I'm not sure if the processing you decribed would be possible unless you had the whole line at once.

    How exactly does it get 'screwed up'


  • Prince of Dhump

    No, we can't safely assume that a System.String can hold Int32.Max characters just because m_stringLength is an int. I seem to remember that the top two bits of the length field are used for storing a couple of flags - meaning that the maximum size would be Int32.Max/2 (the top bit isn't a problem because it would be used for the sign). There was a web page describing this, but I can't remember where...

    The good news is that this isn't really much of a limitation, at least at the moment - you can still have a 2GB string, which is likely to cause you bigger grief than not being able to go beyond it :)

    Jon


  • how long can a string be