Link to home
Start Free TrialLog in
Avatar of theartha
thearthaFlag for United States of America

asked on

How to limit StringBuikder Size

Hi There,

I am creating a StringBuilder in my Constructor. I am trying to read the data and insert in the String Builder. As I am inserting, the StringBuikder Size is growing.

Private Shared MAXLENGTH As Integer = 94

Me.strBuilder = New StringBuilder(New String(" "c, Record.MAXLENGTH))

How can I limit my size to 94 characters even if I insert characters.

Please advice.

Thanks

Avatar of Carlos Villegas
Carlos Villegas
Flag of United States of America image

After append text to your String Builder you can use the Length property to truncate it content:
Me.strBuilder.Length = 94;

Open in new window


Reference:
http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.length.aspx
Remove the ; from the example (C# stuff)
One of the constructors of the StringBuilder lets you specify it's capacity:

strBuilder=New System.Text.StringBuilder(MAXLENGHT)

Mixing that with the code you posted, you might be looking for something like this instead...

strBuilder = New StringBuilder(New String(" "c, Record.MAXLENGTH),MAXLENGTH)

...HOWEVER

Since you initialize it with a String that is already 94 characters long, the StringBuilder is already completely filled in. So any Insert will add one character and it will bomb on you because it will become too long for the specified capacity.

Maybe you should use my first line of code, and built it from scratch.

The second piece of code could be used, but you would need to use Replace instead of Insert, to replace the spaces instead of inserting between them.

Hello @James
If the number of characters to be stored in the current instance exceeds this capacity value, the StringBuilder object allocates additional memory to store them.
Reference:
http://msdn.microsoft.com/en-us/library/zb91weab.aspx
ASKER CERTIFIED SOLUTION
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Naman Goel
You can use something like This

 StringBuilder sb = new StringBuilder(0,MAXLENGHT);

and you can use following method for appending text

 
private bool AppendValueToStringBuilder(StringBuilder sb, string value)
        {
            bool isAppended = false;
            int requieredCapacity=value.Length + sb.Length;
            if (requieredCapacity > sb.MaxCapacity)
            {
                int noOfCharswillbeInserted = requieredCapacity - sb.MaxCapacity;
                sb.Append(value, 0, noOfCharswillbeInserted);
                isAppended = false;
            }
            else if (requieredCapacity <= sb.MaxCapacity)
            {
                sb.Append(value);
                isAppended = false;
            }
            else
            {
                isAppended = false;
            }
            return isAppended;
        }

Open in new window

following is the method is VB.NET:

StringBuilder sb = new StringBuilder(0,MAXLENGHT)

 
Private Function AppendValueToStringBuilder(sb As StringBuilder, value As String) As Boolean
	Dim isAppended As Boolean = False
	Dim requieredCapacity As Integer = value.Length + sb.Length
	If requieredCapacity > sb.MaxCapacity Then
		Dim noOfCharswillbeInserted As Integer = requieredCapacity - sb.MaxCapacity
		sb.Append(value, 0, noOfCharswillbeInserted)
		isAppended = False
	ElseIf requieredCapacity <= sb.MaxCapacity Then
		sb.Append(value)
		isAppended = False
	Else
		isAppended = False
	End If
	Return isAppended
End Function

Open in new window

Avatar of theartha

ASKER

@JamesBurger:

I could limit the size of the Stringbuilder using
Private Shared MAXLENGTH As Integer = 94

Me.strBuilder = New StringBuilder(Record.MAXLENGTH, Record.MAXLENGTH)

but I am unable to insert or append a value at a particular position.

            Dim padLength As Integer = 3
            Dim i As Integer

           Me.strBuilder.Append(CStr(0), startPosition, padLength) ......
' startPosition is 45, padLength is 3. So I am trying to insert 3 zero's at 45,46 and 47th position

Is that possible? Is there any other way.

Thanks.

use

sb.Insert(45,"000")
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: startIndex
this will work only when you are having atleast 45 chars in StringBuilder, because we are trying to insert at 45 position.
I don't have 44 character filled yet. I filled 30 characters and trying to fill 45,46,47 characters.
then Append() should work.
do something like

sb.Append("abc".PadLeft(14,'0'));
If you have a StringBuilder set to a maximum capacity of 94, you won't be able have a String longer than that in the StrinBuilder.

If the String is empty at the start, you will be able to Append into it up to 94 characters. But you will not be able to insert into a position until that position has been filled through Append. You cannot Insert at position 23 if the String is "Hi Everybody", because  there is nothing at position 23. Both New StringBuilder(Record.MAXLENGTH, Record.MAXLENGTH) and New StringBuilder(0, Record.MAXLENGTH) give you that condition.

If the String is completely filled in at the start, such as with New StringBuilder(New String(" "c, Record.MAXLENGTH),MAXLENGTH), then there is no way to Insert or Append anywhere, because the String is already 94 characters long. Any Insert or Append would make it longer than 94, which is forbidden by the second parameter.