Skip to content

How long can a string be?

Jesse

Asked Jul 5, 2006, 12:18 PM EDT1 answer
Share
LinkedInX
How long can a System.String be? I know that the Length() method returns a signed Int32, so it probably can't exceed 2^31 (2 billion something) characters. Does anyone know if its length is further restricted to a smaller maximum?

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • Mike Gold
    0
    Not sure about string, but StringBuilder has  a max capacity of 2,147,483,647.  Here is the constructor:

    C#
    public StringBuilder(int capacity)
    {
          this.m_currentThread = StringBuilder.InternalGetCurrentThread();
          this.m_MaxCapacity = 0;
          this.m_StringValue = null;
          if (capacity < 0)
          {
                throw new ArgumentOutOfRangeException("capacity", string.Format(Environment.GetResourceString("ArgumentOutOfRange_MustBePositive"), "capacity"));
          }
          if (capacity == 0)
          {
                capacity = 0x10;
          }
          this.m_StringValue = string.GetStringForStringBuilder(string.Empty, capacity);
          this.m_MaxCapacity = 0x7fffffff;
    }
    0