Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with StringBuilder

Status
Not open for further replies.

uscboy

Programmer
Sep 16, 2004
4
0
0
US
Hello Group
I have the following StringBuilder object code:

//----Begin CODE----------
StringBuilder record5 = new StringBuilder();
record5.Length = 94; //Set length

record5.Insert(0,"0520"); //Insert at position 0
record5.Insert(4,"4CCC");
record5.Insert(20,"20CCCC");
record5.Insert(40,"40CCCCCC");
record5.Insert(50,"50D");
record5.Insert(53,"53EEEEEE");
//Insert character at the last position
record5.Insert(93,"X");

StreamWriter myFile = File.AppendText("MYFILE.txt");
myFile.WriteLine(record5);
myFile.WriteLine(record5);

myFile.Close();
//---End of Code---

The problem is the characters are not inserted at specified positions, plus there are lot of spaces after the 94th character which is "X" ...
Thus the file is not written in the format I want to. Plus when I print this text file from notepad, there is one blank line between the first 'record5' and then second 'record5'. I just can't figure out why ?! Please help.

Thanks in advace.
C# Newbie !
 
As far as the length being 94 it is because you started with 94 and then INSERTed characters which will push out existing characters beyond offset 93.

If you want 94 then set it again after all the inserts.
record5.Length = 94; //Set length


Forms/Controls Resizing/Tabbing
Compare Code
Generate Sort Class in VB
Check To MS)
 
Hi John

Thanks a lot for your reply. But if I try to set the length after all Inserts (which I had tried earlier), I am getting the SystemOutOfRange Exception cause by default the capacity is set to 16 characters. To counter this,I had set the Capacity to 94 and also did EnsureCapacity(94) to check that. But if I dont have the Length set to 94 before inserts I get the OutOfRange Exception.

am still confused.
 
So they lied.
"Remarks
If the specified length is less than the current length, this instance is truncated to the specified length. If the specified length is greater than the current length, the end of the string value of this instance is padded with spaces (Lie. It is padded with 0x0). If the specified length is greater than the current capacity, Capacity is set to the specified length."


I stepped through the following:
StringBuilder record5 = new StringBuilder();
record5.Length = 94; //Set length
int i;
for (i=0;i < record5.Length;i++)
{
byte byt = Convert.ToByte(record5);
if (byt < 32) break;
}
.....

a 0x0 in a StringBuilder is treat as a string terminator.

Try

StringBuilder record5 = new StringBuilder();
record5.Append(' ',94);
record5.Insert(0,"0520"); //Insert at position 0
record5.Insert(4,"4CCC");
record5.Insert(20,"20CCCC");
record5.Insert(40,"40CCCCCC");
record5.Insert(50,"50D");
record5.Insert(53,"53EEEEEE");
//Insert character at the last position
record5.Insert(93,"X");
record5.Length(' ',94);




Forms/Controls Resizing/Tabbing
Compare Code
Generate Sort Class in VB
Check To MS)
 
Hi John

I just used the "record5.Append(' ',94);" and then Inserted the characters I wanted to.
It worked fine.

Oh, but one thing, "record5.Length('',94); " in the end does not work , however,if I use record5.Length=94; , it works.

Thanks for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top