newReceivedData = myComPort.ReadExisting()
' Append the String to the StringBuilder object.
stringBuffer.Append(newReceivedData)
using System.Text;
internal StringBuilder stringBuffer = new StringBuilder();
String newReceivedData;
// Read received data into a String.
newReceivedData = myComPort.ReadExisting();
// Append the String to the StringBuilder object.
stringBuffer.Append(newReceivedData);
To convert a StringBuilder object to a String, use the ToString property:
Dim receivedText as String
receivedText = stringBuffer.ToString
String receivedText;
receivedText = stringBuffer.ToString();
To clear a StringBuilder object, use the Remove method:
stringBuffer.Remove(0, stringBuffer.Length)
stringBuffer.Remove(0, stringBuffer.Length);
Managing Ports and Transfers in .NET
199
The Remove method can also remove selected characters:
' Remove the first two characters:
stringBuffer.Remove(0, 2)
// Remove the first two characters:
stringBuffer.Remove(0, 2);
You can specify a capacity when declaring a StringBuilder object:
Friend stringBuffer As New StringBuilder(1024)
internal StringBuilder stringBuffer = new StringBuilder(1024);
The StringBuilder object can store up to the specified capacity without having
to allocate more memory. On exceeding the specified capacity, the object grows
in size as needed to hold the data.
Pages:
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232