Sunday, July 9, 2017

REXX: Built in Word Functions

A word is defined as a blank delimited set of characters within a string in REXX. The characters in a word can be alpha, numeric, alphanumeric, or hex, or binary or special characters as the REXX interpreter does not differentiate between them within a string.

Word functions are designed to process words within a string. 

Some of the word functions and their descriptions are given in the below table. 

Function
Description
SUBWORD
Returns a substring of a given string from a specified word for a specified number of words.
WORD
Returns the nth specified word in a string.
WORDINDEX
Returns the position of the first character in a specified word in a string.
DELWORD
Deletes one or more words, starting a specified word
WORDLENGTH
Returns the length of a specified word
WORDPOS
Returns the position of a specified word or phrase in a given string
WORDS
Returns the number of words in a given string
FIND
Returns the word number of the first occurrence of a given string in a phrase. WORDPOS is usually preferred.

WORDS:
This function returns the number of words in a string
Syntax: WORDS(string)

Examples: 

WORDS('I am a hero')    /* returns 4*/
WORDS('1 2 3 4 ')          /* returns 4*/
WORDS('! @ # $ ')          /* returns 4*/
WORDS('a b c d ')          /* returns 4*/

WORD:
This function returns the nth word from a string. 

Syntax: WORD(string,n)

Examples: 

WORD('(I am a hero',2)                /* returns 'am' */

Coding word(string,n) is the same as coding subword(string,n,1)

WORDPOS:

This function returns the word number of the first word of phrase from string. If the start is specified, it identifies the word in string at which to begin the search. If phrase is not found, WORDPOS returns 0. 

The FIND function could be used instead of WORDPOS, but the syntax is slightly different, for example:

FIND(string,phrase,start)

WORDPOS is preferred for standardization reasons. 

Syntax: WORDPOS(phrase,string,start)

Examples:

WORDPOS('am a','i am a hero')         /*returns 2*/
WORDPOS('am a','i am a hero',2)      /*returns 2*/
WORDPOS('am a','i am a hero',3)      /*returns 0*/

SUBWORD:

This function is similar to the SUBSTR function, except the n specifies the nth word of the string, and length specifies the number of words in the returned string. If length is not specified, it defaults to the rest of the words in the string. 

Specifying SUBWORD(string,4,1) is same as specifying with WORD function like WORD(string,4)

Syntax: SUBWORD(string, n, length) 

Examples:

SUBWORD('I am a hero',2)    /*returns 'am a hero'  */
SUBWORD('I am a hero',2,2) /*returns 'am a'         */ 

WORDINDEX:
This function returns character position of the first character of word n from string.

If there are fewer than n words in string, WORDINDEX is 0. 

Syntax: WORDINDEX(string, n)

Examples: 

WORDINDEX('i am a hero',2)    /*returns 3*/
WORDINDEX('i am a hero',4)    /*returns 8*/   

WORDLENGTH:

This function returns the length of word n from string

If there are fewer than n words in string, WORDLENGTH returns 0.

Syntax: WORDLENGTH(string,n)

Examples:  

WORDLENGTH('i am a hero',2)     /*returns 2*/

WORDLENGTH('i am a hero',2)     /*returns 4*/  

DELWORD:

This function returns length words deleted from the string, starting at word n.

If length is not specifed, all words from n are deleted.

Syntax: DELWORD(string,n, length)

Examples:

DELWORD('i am a hero',3)     /* returns ' i am ' */
DELWORD('i am a hero',3,1)  /* returns ' i am hero' */

No comments:

Post a Comment

Featured Post

REXX Skeleton: Submitting jobs through Rexx

Submitting jobs through REXX and reading the spool through REXX gives us immense potential to automate many manual activities in mainframes....