Character (Chr)

What is it?

The Chr function is similar to the Substr function we discussed above, but instead of extracting a multi-character substring, it returns a single character from the index you specify.

What is the Syntax?

The Chr function’s syntax is as follows:

Chr( String, Index );

Why might I want to use this?

As with the Substr function, you can use the Chr function to create shortened versions of other strings. If you wanted to create a user ID containing the surname and initial letter of a person’s forename, you could use the following expression:

UserID := Surname & Chr( Forename, 0 );

In this case, John Smith’s user ID would be “SmithJ”.

You might also use the Chr function to extract a random character from a customer’s 10-character pass code for security purposes. To choose a random index, you would use the Rnd function combined with the Floor function in a (similar to how we modelled the roll of a six-sided die earlier in the guide). In this case we use the Floor function instead of the Ceil function, since we need a number between 0 and 9, not between 1 and 10.

Floor( Rnd() * 10 );

Next, we include this expression as part of our Chr function. Now, the expression evaluates to a random character from our passcode:

RandomDigit := Chr( PassCode, Floor( Rnd() * 10 ) );

 

<< Previous: Substring (Substr) | Next: Trim >>