Click or drag to resize

BinaryTextEncodingStringDelimited Method

Returns an encoder that will encode and decode string-delimited strings given a character encoding.

Namespace:  Demo3D.IO
Assembly:  Demo3D.IO (in Demo3D.IO.dll) Version: 11.0.0.7658
Syntax
C#
public static BinaryTextEncoding StringDelimited(
	string header,
	string footer,
	Encoding characterEncoding = null
)

Parameters

header
Type: SystemString
The expected string header (or null).
footer
Type: SystemString
The string footer/delimiter.
characterEncoding (Optional)
Type: System.TextEncoding
The character encoding. (Defaults to ASCII.)

Return Value

Type: BinaryTextEncoding
An instance of BinaryTextEncoding that will encode and decode strings in this format.
Examples

The following example shows simple examples for reading and writing strings.

C#
public void StringDelimitedStrings(IDataReader receivedPacket, IDataWriter packetToSend) {
    // The string to be read will start with '<<' and end with '>>'.  The string returned will have the
    // quotes ('<<' and '>>') stripped off.  Just the original string between the quotes will be returned.
    var str = receivedPacket.ReadString(BinaryTextEncoding.StringDelimited("<<", ">>"));

    // The string is terminated with a '|' character.  Effectively the protocol uses strings separated with '|'.
    str = receivedPacket.ReadString(BinaryTextEncoding.StringDelimited(null, "|"));

    // The standard STX ETX encapsulated format.
    const string stx = "\u0002";
    const string etx = "\u0003";
    str = receivedPacket.ReadString(BinaryTextEncoding.StringDelimited(stx, etx));

    // Writes the string "hello" prepended with the STX character and appended with the ETX character.
    packetToSend.WriteString("hello", BinaryTextEncoding.StringDelimited(stx, etx));
}
See Also