Skip to main content

StringUtils


id: py-StringUtils

StringUtils

Utility class for common string operations: encoding, decoding, validation, and charset conversion. All methods are static.


At a Glance

# Validation
if StringUtils.isNullOrBlank(message.getString(dataDictionary.type.Customer.EMAIL)):
message.addStatus(Severity.ERROR, Status.create(VENDOR, 'EMAIL_REQUIRED'))

# Encoding
bytes_arr = StringUtils.toBytes("Hello, World!")
base64 = StringUtils.base64Encode(bytes_arr)

# Decoding
decoded = StringUtils.base64Decode(base64)
text = StringUtils.fromBytes(decoded)

Validation

MethodReturnsDescription
isNullOrEmpty(value)boolTrue if None or empty string ""
isNullOrBlank(value)boolTrue if None, empty, or whitespace only
isNumeric(value)boolTrue if string contains only digits (0–9)
StringUtils.isNullOrEmpty(None) # True
StringUtils.isNullOrEmpty("") # True
StringUtils.isNullOrEmpty(" ") # False

StringUtils.isNullOrBlank(None) # True
StringUtils.isNullOrBlank(" ") # True

StringUtils.isNumeric("12345") # True
StringUtils.isNumeric("12.3") # False
StringUtils.isNumeric("abc") # False

Encoding & Decoding

base64Encode(bytes)

Encodes a byte array to a Base64 string.

ParameterTypeDescription
bytesList[int]Byte array to encode

Returns: str

bytes_arr = StringUtils.toBytes("Hello")
base64 = StringUtils.base64Encode(bytes_arr) # "SGVsbG8="

base64Decode(value)

Decodes a Base64 string to a byte array.

ParameterTypeDescription
valuestrBase64 string to decode

Returns: List[int]

bytes_arr = StringUtils.base64Decode("SGVsbG8=")
text = StringUtils.fromBytes(bytes_arr) # "Hello"

Charset Conversion

toBytes(value, charset?)

Converts a string to a byte array.

ParameterTypeDescription
valuestrString to convert
charsetstr (optional)Charset — defaults to UTF-8

Returns: List[int]

bytes_arr = StringUtils.toBytes("Hello") # UTF-8
ascii = StringUtils.toBytes("Hello", "US-ASCII") # ASCII

fromBytes(bytes, charset?)

Converts a byte array to a string.

ParameterTypeDescription
bytesList[int]Byte array to convert
charsetstr (optional)Charset — defaults to UTF-8

Returns: str

text = StringUtils.fromBytes(bytes_arr) # UTF-8
text2 = StringUtils.fromBytes(bytes_arr, "ISO-8859-1") # Latin-1

Supported Charsets

CharsetDescription
US-ASCII7-bit ASCII
ISO-8859-1Latin-1
UTF-8Default, 8-bit Unicode
UTF-1616-bit Unicode (with BOM)
UTF-16BEBig-endian UTF-16
UTF-16LELittle-endian UTF-16

Complete Example

def on_message():
email = message.getString(dataDictionary.type.Customer.EMAIL)

# Validate
if StringUtils.isNullOrBlank(email):
message.addStatus(Severity.ERROR, Status.create(VENDOR, 'EMAIL_REQUIRED'))
elif '@' not in email:
message.addStatus(Severity.ERROR, Status.create(VENDOR, 'EMAIL_INVALID'))

# Encode payload for external API
import json
payload = json.dumps({"email": email})
bytes_arr = StringUtils.toBytes(payload)
encoded = StringUtils.base64Encode(bytes_arr)
message.setString(dataDictionary.type.Customer.ENCODED_PAYLOAD, encoded)

stream.emit(message, OUTPUT_PORT)

See Also