CONVERT, CONVERT() |
![]() ![]() ![]() |
The CONVERT statement and CONVERT() function replace selected characters by others in a string. The CONVERT statement performs this conversion in-situ; the CONVERT() function leaves the source string unchanged and returns the modified value.
Format
CONVERT from.string TO to.string IN var CONVERT(from.string, to.string, source.string)
where
The statement
S = CONVERT(X, Y, S)
is equivalent to
CONVERT X TO Y IN S
Characters taken from the from.string and to.string define character translations to be performed. Each occurrence of a character from from.string in var (or src.string) is replaced by the character in the same position in to.string. If to.string is shorter than from.string, characters for which there is no replacement character are deleted. If to.string is longer than from.string the surplus characters are ignored.
If a character appears more than once in from.string only the first occurrence is used.
If the $NOCASE.STRINGS compiler directive is used, matching of from.string against var is case insensitive.
Examples
S = "ABCDEFGHIJK" CONVERT "CGAGJ" TO "123" IN S
This program fragment replaces all occurrences of the letter "C" in S by "1", "G" by "2" and "A" by "3". The second occurrence of "G" in the from.string is ignored. The letter "J" is deleted from S. The result of this operation is to set S to "3B1DEF2HIK".
PRINT CONVERT(" ", "#", S)
This statement prints the string S with all spaces replaced by # characters.
LOOP INPUT ISBN,13_: UNTIL CONVERT('0123456789X-', '', ISBN) = '' INPUTERR 'Invalid ISBN' REPEAT
The loop above verifies that the data entered by the user contains only digits, the letter X and hyphens. The CONVERT() function is used to return a copy of the input data with all valid characters removed. If the result string is not null, it must contain an invalid character.
See also: |