Previous Next Chapter

Example Program

The following example program illustrates many of the built-in functions that manipulate character strings.

Program 13. Changestrings.rexx

/*This ARexx program shows the effect of built-in functions that change strings. The functions come in two groups: one that manipulates individual characters and one that manipulates whole strings.*/

teststring1 = " every good boy does fine "

/*The first group is composed of the functions STRIP(), COMPRESS(), SPACE(), TRIM(), TRANSLATE(), DELSTR(), DELWORD(), INSERT(), OVERLAY(), and REVERSE().*/

/*STRIP() removes only leading and trailing characters.*/

/*Print the original string, for comparison. We put a period at the end of the string, so you can see what happens to the spaces at the end of the string.*/

SAY " every good boy coes fine "

/*The same string stripped of leading and trailing spaces*/

SAY STRIP(" every good boy does fine ")"."

/*Failed attempt to remove leading and trailing "e"s*/

SAY STRIP(" every good boy does fine",,"e")"."

/*The "e"'s were protected by the leading and trailing spaces. Removing them exposes the "e"'s to the effects of STRIP()*/

SAY STRIP("every good boy does fine",,"e")"."

/*Remove "e"'s and spaces from the original string*/

SAY STRIP(" every good boy does fine ",," e")"."

/*We are now using the variable "teststring1", defined above. Remove only the trailing spaces in the test string.*/

SAY STRIP(teststring1, T)"."

/*Remove the trailing spaces and the "e"*/

SAY STRIP(teststring1,T," e")"."

/*Compress() removes characters anywhere in the string. This removes all blanks from the test string*/

SAY COMPRESS(teststring1)

CALL TIME(`r')

SAY TIME(`Civil') /*Civilian time HH:MM{AM | PM}*/

SAY TIME(`h') /*Hours since midnight*/

SAY TIME(`m') /*Minutes since midnight*/

SAY TIME(`s') /*Seconds since midnight*/

SAY TIME(`e') /*Elapsed time since program start*/

/*Function:TRACE Usage: TRACE( [option] )*/

SAY TRACE()

SAY TRACE(TRACE()) /*Leave it unchanged*/

/*Function:TRANSLATE Usage: TRANSLATE(string[,output][,input] [,pad])*/

SAY TRANSLATE(`aBCdef') /*Translate to Uppercase*/

SAY TRANSLATE (`abcdef', `1234')

SAY TRANSLATE(`654321', `abcdef', `123456')

SAY TRANSLATE(`abcdef', `123', `abcdef', `+')

/*Function: TRIM Usage: TRIM(string)*/

SAY TRIM(` abc `)

/*Function: TRUNC Usage: TRUNC(number[,places])*/

SAY TRUNC(123.456)

SAY `$'TRUNC(134566.123,2)

/*Function: UPPER Usage: UPPER(string)*/

SAY UPPER(`aBCdef12')

/*Function: VALUE Usage: VALUE(name)*/

abc = `my name'

SAY VALUE(`abc')

/*Function: VERIFY Usage: VERIFY(string,list[,'M'])*/

SAY VERIFY(`123a45', `0123456789')

SAY VERIFY(`abc3de', `012456789', `M')

/*Function: WORD Usage: OWRD(string,n)*/

SAY WORD(`Now is the time',3)

/*Function: WORDINDEX Usage: WORDINDEX(string,n)*/

SAY WORDINDEX(`Now is the time `,3)

/*Function: WORDLENGTH Usage: WORDLENGTH(string,n)*/

SAY WORDLENGTH(`Now is the time `,4)

/*Function: WORDS Usage: WORDS(string)*/

SAY WORDS(`Now is the time')

/*Function: WRITECH Usage: WRITECH(logical,string)*/

IF OPEN(`test','ram:test$$', `W') THEN DO

SAY WRITECH(`test', `message') /*Write the string*/

CALL CLOSE `test'

END

/*Function: WRITELN Usage: WRITELN(logical,string)*/

IF OPEN(`test', `ram:test$$', `W') THEN DO

SAY WRITELN(`test', `message')

/*Write the string (with newline)*/

CALL CLOSE `test'

END

/*Function: X2C Usage: X2C(heystring)*/

SAY X2C(`616263') /*Convent to character (pack)*/

/*Function: XRANGE Usage: XRANGE([start] [,end])*/

SAY C2X(xrange(`f0'x))

SAY XRANGE(`a', `g')

EXIT

The output of Program 13 is:

every good boy does fine
every good boy does fine.
every good boy does fine .
very good boy does fin.
very good boy does fin.
every good boy does fine.
every good boy does fin.
everygoodboydoesfine
1:23PM /*These results vary depending*/
13 /*on the time the program is run.*/
803
48199
0.80
N
N
ABCDEF
abcdef
fedcba
123+++
abc
123
$134566.12
ABCDEF12
my name
4
0
the
8
4
4
7
8
abc
F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF
abcdefg

Top Previous Next Chapter