Testing character strings for multi-byte characters
To determine whether a character string contains multi-byte characters, use the LENGTH function, which returns the number of characters, bytes, or columns in a string. The syntax is:
LENGTH ( {string[type]|raw-expression} )
string
A character expression. The specified string can contain double-byte characters.
type
A character expression that indicates whether you want the length of a string in character units, bytes, or columns. A double-byte character registers as one character unit. The default unit of measurement is character units.
There are three valid types: CHARACTER, RAW, and COLUMN. The expression "CHARACTER" indicates that the length is measured in characters, including double-byte characters. The expression "RAW" indicates that the length is measured in bytes. The expression "COLUMN" indicates that the length is measured in columns. If you specify the type as a constant expression, OpenEdge validates the type specification at compile time. If you specify the type as a variable expression, OpenEdge validates the type specification at run time.
raw-expression
A function or variable name that returns a raw value.
To use the technique, call LENGTH twice: once with the CHARACTER option, which returns the length in characters, and once with the RAW option, which returns the length in bytes. Then, compare the two lengths. If they are equal, the string contains only single-byte characters; otherwise, the string contains at least one multi-byte character.
The following examples illustrate the technique The first example tests a character string consisting of one double-byte character. Since the length of the string in characters (1) does not match the length in bytes (2), the example displays Multi-byte characters in the string:
DEFINE VARIABLE mychar AS CHARACTER INITIAL "" NO-UNDO.
IF LENGTH(mychar,"CHARACTER") = LENGTH(mychar,"RAW") THEN
DISPLAY "No multi-byte characters in the string".
ELSE DISPLAY "Multi-byte characters in the string".
The second example tests a character string consisting of three single-byte characters. Since the length of the string in characters (3) matches the length in bytes (3), this example displays No multi-byte characters in the string:
DEFINE VARIABLE mychar AS CHARACTER INITIAL "123" NO-UNDO.
IF LENGTH(mychar,"CHARACTER") = LENGTH(mychar,"RAW") THEN
DISPLAY "No multi-byte characters in the string".
ELSE DISPLAY "Multi-byte characters in the string".