IsValidUtf8 Method  
 

Checks if a byte array contains structurally valid UTF-8 encoded text.

Syntax

object.IsValidUtf8( InputBytes )

Parameters

InputBytes
A byte array which is checked to ensure that it contains only valid UTF-8 encoded characters. No other data types are accepted by this method and an exception will be thrown if an invalid data type is provided as the input text.

Return Value

If the byte array contains valid UTF-8 text, it returns True. If invalid UTF-8 characters are encountered in the input, or the array does not contain any characters, the method returns False.

Remarks

This method checks a byte array to ensure that it contains structurally valid UTF-8 encoded text. If the input is an empty array, the method returns False to indicate that actual content is required for validation. If a byte-order mark (BOM) is present at the start of the array and no additional characters follow, the method still returns True. The BOM itself is considered valid Unicode text.

This method does not verify that the data is readable or printable, it only ensures that the input conforms to the UTF-8 encoding rules. Invalid byte sequences, such as overlong encodings or unpaired continuation bytes, will cause the method to return False.

Use this method when you need to verify that raw binary input is UTF-8 encoded. For example, if you read the contents of a file or receive data from an external source (such as a web API, database, or email message) and need to confirm it is valid UTF-8 before attempting to decode it as a string, this method provides a fast, safe check.

If you wish to remove unwanted or non-printable characters from UTF-8 text, use the FilterUnicode method instead.

See Also

FilterUnicode, StrToUtf8 Method, Utf8ToStr Method