piece Method

Class: ByteString

Description

This method extracts one or more elements from a delimited byte string.

Syntax

ByteStringObject.piece(ByteString delimiter, Number first, Number last)

Parameters

delimiter

The byte string that is used to separate elements within the byte string.

first

The first delimited piece of the byte string to extract. The default value is 1.

last

The last delimited piece of the byte string to extract. The default value is the value of first.

Returns

A Byte String

When Became Available

Profile V7.0

Example

type String     STR = "the kNäckerkNödle" // contains 2 two-byte characters

type ByteString BST = STR.toByteString() // the string as byte string

type ByteString DEL1 = "kN".toByteString() // a two byte delimiter

type ByteString DEL2 = "N".toByteString()_ 195.byte() // also two byte

 

// examples with DEL1 are "String safe":

// a well-formed String will remain well-formed

type ByteString BS1 = BST.piece( DEL1, 2,3) // BS1 = "äckerkNödle"

type String STOK = {String}BS1 // no %GTM-E runtime ever!

 

// examples with DEL2 are not "String safe":

// the delimiter will result in partial byte sequences that will never

// result in a well-formed String (except if BST'[DEL2)

type ByteString BS2 = BST.piece( DEL2, 2) // start of BS2 is invalid

write BS2.ascii() // writes 164

type String STERR = {String}BS2 // likely %GTM-E runtime!