StringBuilder class

I am attempting to use the stringbuilder class as a command buffer for commands that are being received via TCP. Since there is no guarantee that the entire command will be received on the server end, I have my receive thread simply append the command to a variable of type Stringbuilder. This works fine to continue to build the buffer but there are not any methods to find and pull out the command string from the buffer.

At this point, I am planning to use the Instr function to find the command termination sequence and then use the Left function to retrieve the complete command. Each of these functions require a string as input so I am planning to constantly use the .ToString method of the stringbuilder.

Am I missing something, or is this probably the best way to accomplish what I want to do? I know that I could simply use a string variable, but since string variables always create a new instance of the variable upon concatenation, I am concerned that the stack would start to bog down after the application has been running for some time.

Guess it would be nice if the stringbuilder class would provide some addtional methods that cloned the Instr, StrComp, Left, Right, and Mid functions.

Tony

[1262 byte] By [awperli] at [2007-12-21]
# 1

That's not what stringbuilder is for. You can call ToString at any time and use those methods on the string class.

cgraus at 2007-9-10 > top of Msdn Tech,.NET Development,Common Language Runtime...
# 2

Instead of constantly using ToString(), wouldn't it make sense to use

string s= sb.ToString();

//use s with Instr, Left etc..

ArtySaravana at 2007-9-10 > top of Msdn Tech,.NET Development,Common Language Runtime...
# 3

I didn't mean NOT to do that, but either way, it would depend on what he's going to do. It may be more efficient, it may achieve nothing. It couldn't hurt.

cgraus at 2007-9-10 > top of Msdn Tech,.NET Development,Common Language Runtime...
# 4

Chris,

Actually, I think that the string builder would be suited perfectly for this since I am constantly appending if only it had some search and a remove method that returned the removed section to you.

I read the help on the string and stringbuilder classes and I am concerned about the following statement:

A String object concatenation operation always creates a new object from the existing string and the new data. (In other words, on the stack)

Whereas:

A StringBuilder object maintains a buffer to accommodate the concatenation of new data.

The only thing that I want to do is be able to search and pull out pieces of the buffer. So that I can remove complete commands while adding incoming data to the back.

Tony

awperli at 2007-9-10 > top of Msdn Tech,.NET Development,Common Language Runtime...

.NET Development

Site Classified