CONVERT

CONVERT, CONVERT()

Top  Previous  Next

 

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.chars TO to.string IN var

CONVERT(from.chars, to.string, source.string)                See below for argument sequence

 

where

 

from.charsevaluates to a string containing the characters to be replaced.

 

to.charsevaluates to a string containing the replacement characters.

 

varis the variable in which the in-situ replacement is to occur.

 

source.stringis the string to be modified.

 

 

The statement

 

S = CONVERT(X, Y, S)

 

is equivalent to

 

CONVERT X TO Y IN S

 

Characters taken from from.chars and to.chars define character translations to be performed. Each occurrence of a character from from.chars in var (or source.string) is replaced by the character in the same position in to.chars. If to.chars is shorter than from.chars, characters for which there is no replacement character are deleted. If to.chars is longer than from.chars the surplus characters are ignored.

 

If a character appears more than once in from.chars only the first occurrence is used.

 

If the $NOCASE.STRINGS compiler directive is used, matching of from.chars against var is case insensitive.

 

 

Migration note: The default behaviour has the function argument order as in Information style systems which is different the equivalent function in Pick style systems. Use of the PICK.CONVERT setting of the $MODE compiler directive changes the argument order to be

CONVERT(source.string, from.chars, to.chars)

This alternative argument ordering is not available in dictionary I-type expressions.

 

 

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.chars 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:

CHANGE()