translate Method

Class: ByteString

Description

This method returns a byte string that results from replacing or dropping bytes in its first parameter as specified by the pattern in its second parameter.

 

Use this method with extreme care. Ensure that both parameters are properly byte-based (and byte-aligned).

Syntax

ByteStringObject.translate(ByteString lookFor, ByteString replaceBy)

Parameters

lookFor

The bytes to look for.

replaceBy

The bytes to replace when a matching byte is found.

Returns

A ByteString

When Became Available

Profile V7.0

Example

type ByteString BS1 = "Knäckebröd".toByteString()

 

// Some harmless examples

type ByteString LF1 = "Kb".toByteString()

type ByteString RB1 = "kB".toByteString()

type ByteString RES1 = BS1.translate( LF1) // näckeröd

type ByteString RES2 = BS1.translate( LF1, RB1) // knäckeBröd

 

// Examples that will return a ByteString with invalid sequences

type ByteString LF2 = "är".toByteString // NOTE: 3 bytes (195,164,114)

type ByteString RB2 = "aR".toByteString() // NOTE: 2 bytes ( 97, 82)

type ByteString RB3 = "Rö".toByteString() // NOTE: 3 bytes ( 82,195,182)

 

// Note that every occurrence of #195 will be replaced by a lowercase "a",

// which will replace the first of the 2 bytes that represent the ö, but

// leaves the second byte. The resulting value contains an invalid sequence

//

type ByteString BX1 = BS1.translate( LF2, RB2) //"KnaRckeba"_$ZC(182)_"d"

 

// Similarly, using LF2 and RB3:

// - every occurrence of #195 will be replaced by a lowercase "a"

// - every occurrence of #168 will be replaced by #195, and

// - every lowercase "r" will be replaced by #182

// The resulting value contains three invalid sequences:

// "Kna"_$ZC(195)_"ckeb"_$ZC(182)_"a"_$ZC(182)_"d"

//

type ByteString BX1 = BS1.translate( LF2, RB3)