Using REXX String Manipulation Commands

ID: Q99066


The information in this article applies to:
  • Microsoft LAN Manager, versions 2.1, 2.1a, 2.2


SUMMARY

This article lists the string manipulation capabilities of the REXX programming language and gives examples.


MORE INFORMATION

DISCLAIMER: This article is provided for users capable of developing programs with the information presented; it is not an extensive treatment of the language but rather a quick reference to aid someone in writing simple routines. Microsoft cannot support programming efforts beyond reproducing and submitting problems with the language implementation itself. If you need further assistance, consult REXX references such as "The REXX Language, A Practical Approach to Programming" by M. F. Cowlishaw, Prentice-Hall, Englewood Cliffs, 1985.

Examples are given at the bottom of the listing. Braces ({}) are used to indicate comments.


ABBREV(string, test_str, length)

   { If test_str is the same as the leading characters of string and
   test_str is at least "length" long then ABBREV returns 1, otherwise
   zero. }

CENTER(string, final_length, pad_character_if_needed)

   { Center the string in a final_length field padded on each sides
   with the specified character if needed. If the string is longer
   than the final_length characters will be truncated from each end. }

COMPARE(string1, string2, pad)

   { Returns zero or the position of the first non-match. For
   differing length strings "pad" is compared to the remaining part of
   the longer string. The default for "pad" is space. }

COPIES(source_string, number)

   { Creates a new string of "number" repetitions of source_string. }

DELSTR(string, start_position, number_of_bytes_to_delete)

   { Returns a string with the number of bytes deleted at the start
   position but does not alter "string" itself. }

DELWORD(string_of_words, start_word, #_of_words_to_delete)

   { Same as DELWORD except on the word level. If the number of words
   is not specified then delete to end-of-line. }

INSERT(source, target, start, length, pad_character)

   { Insert "length" of the source into the target at the position
   specified by "start" padding with pad_character if necessary. }

LASTPOS(find_this, target_string, search_backward_from_here)

   { Search backward from the end of target_string or specified
   position looking for find_this and return the position or zero if
   not found. }

LEFT(of_this_string, for_this_length, pad_with_this_if_reqd)

   { Returns the leftmost bytes from of_this_string and pads the end
   with pad_with_this_if_reqd to produce a string of for_this_length
   bytes. }

LENGTH(of_this_string)

   { returns the length of the specified string. }

OVERLAY(source, target, start_point, length, pad_with_this)

   { returns a target of "length" plus start_point minus one (padded
   with pad_with_this if necessary) with source inserted at
   start_point. }

PARSE [UPPER] [options] [WITH] var1 var2 var3 ... var?|template

   { Parses, optionally uppercasing (with UPPER), the supplied
   [options] placing the result into one or more variables. [options]
   are: ARG, PULL, LINEIN, SOURCE, VALUE expression, VAR name, or
   VERSION. For ARG, PULL, LINEIN, and VALUE, see their descriptions
   in the REXX articles for more information. SOURCE returns the
   operating system (OS/2), source of execution (typically COMMAND for
   the command prompt), and name of the currently executing REXX file
   (*.CMD). VAR name instructs PARSE to parse the named variable.
   VERSION returns the REXX product name, version and date. The
   expression used with VALUE can be a built-in function such as
   DATE(). WITH can be used in such a way that it breaks a string into
   its components (as follows): PARSE VAR source_string first_word
   source_string. "template" is a combination of variables and
   literals, if literals are supplied they control how variables are
   assigned their contents, see example. }

POS(find_this, target_string, start_position)

   { Searches target_string for find_this and returns either the
   located position or zero if find_this is not found. }

REVERSE(string)

   { Returns the string in reversed (the last byte is first, etc.)
   order. }

RIGHT(string, length, pad)

   { Returns the rightmost characters of "string" up to "length".
   "pad" will be used to prepend to string if it is shorter than
   "length". }

SPACE(string, n, pad_character)

   { Isolates words in a string and returns a reconstructed string
   containing the words (in the same order) separated by "n" spaces or
   pad_characters. "n" defaults to 1. }

STRIP(string,'L'|'T'|'B', optional_character_to_strip)

   { Strips 'L'eading, 'T'railing, or 'B'oth leading and trailing
   spaces (or optional_character_to_strip if specified) from string. }

SUBSTR(string, start_position, length, pad_with_this)

   { Returns a portion of "string" beginning at start_position which
   is "length" long, "length" is either satisfied from "string" itself
   or by padding the result with "pad_with_this". }

SUBWORD(string, start_word, number_of_words)

   { This command is virtually identical to SUBSTR except that words,
   not characters, are specified by parameters 2 and 3. }

TRANSLATE(string, replace_with, replace_this)

   { Returns a string with any characters in replace_this changed to
   their corresponding characters in replace_with. For example,
   TRANSLATE("This is a test", "123", "iae") would produce "Th1s 1s 2
   t3st". }

VERIFY(string, reference_list, 'M'|'N', start_position)

   { Returns the position in string of the first character that either
   'M'atchs or does 'N'ot match the characters in reference_list
   (depending on whether 'M' or 'N' was specified). Otherwise a zero
   is returned. The check can be restricted to begin at
   start_position. }

WORD(string, word_number)

   { Returns the word_number'd word or nothing if word_number exceeds
   the number of the last word in the string. }

WORDINDEX(string, word_number)

   { Returns the character position of the word_number word in string.
   }

WORDLENGTH(string, word_number)

   { Returns the length of the word_number word in string. }

WORDPOS(word|phrase, string, start_position)

   { Returns the number of the first (or only) word in phrase relative
   to the start of the string, start_position can be specified as the
   word number to begin with, a zero return means not found. }

WORDS(string)

   { Returns the number of words in a string. } 
Examples follow. Create a REXX program and run it to see the results.

x = "The quick brown fox jumps over the lazy dog"
say 'x = ' x
say "ABBREV(x, 'The quick',8) returns:"
say ABBREV(x, 'The quick',8)
say "ABBREV(x, 'The quick',12) returns:"
say ABBREV(x, 'The quick',12)
say "CENTER(' This ',10,'!') does: " CENTER('This',10,'!')
say "COMPARE('abcde','abcdf') returns:"
say COMPARE('abcde','abcdf')
say "COMPARE('abcde','abcde') returns:"
say COMPARE('abcde','abcde')
say "COMPARE('abcde','abcd',' ') returns:"
say COMPARE('abcde','abcd',' ')
say "COMPARE('abcd ','abcd',' ') returns:"
say COMPARE('abcd ','abcd',' ')
say "COPIES('123',5) returns " COPIES('123',5)
say "DELSTR(x,5,6) returns:"
say DELSTR(x,5,6)
say "DELWORD(x,4,2) returns:"
say DELWORD(x,4,2)
pause
say 'x = ' x
say "INSERT('!',x,5,3,'*') returns:"
say INSERT('!',x,5,3,'*')
say "POS('fox',x) returns " POS('fox',x)
say "POS('fox',x,18) returns " POS('fox',x,18)
say "LASTPOS('e',x) returns " LASTPOS('e',x)
say "LASTPOS('e',x,10) returns " LASTPOS('e',x,10)
say "LEFT(x,19) returns " LEFT(x,19)
say "LEFT(x,50,'-') returns " LEFT(x,50,'-')
say "LENGTH(x) returns " LENGTH(x)
say "OVERLAY('XXX',x,11,5,'&') returns:"
say OVERLAY('XXX',x,11,5,'&')
say "REVERSE(x) returns:"
say REVERSE(x)
say "RIGHT(x,8) returns " RIGHT(x,8)
say "RIGHT(x,50,'^') returns:"
say RIGHT(x,50,'^')
pause
say 'x = ' x
y = 'This is a string'
say "y = 'This is a string' "
say "SPACE(y,2,'_') returns " SPACE(y,2,'_')
z = '*** Note this! ***'
say "z = '*** Note this! ***' "
say "STRIP(z,'L','*') returns " STRIP(z,'L','*')
say "STRIP(z,'T','*') returns " STRIP(z,'T','*')
say "STRIP(z,'B','*') returns " STRIP(z,'B','*')
say "SUBSTR(x,21,5) returns " SUBSTR(x,21,5)
say "SUBSTR(x,41,15, '#') returns " SUBSTR(x,41,15,'#')
say "SUBWORD(x,5,2) returns " SUBWORD(x,5,2)
say "TRANSLATE(x,'W','e') returns:"
say TRANSLATE(x,'W','e')
say "VERIFY(x,'obfj','M') returns " VERIFY(x,'obfj','M')
say "VERIFY(x,'Thequickbrownfox','N') returns:"
say VERIFY(x,'Thequickbrownfox','N')
say "WORD(x,7) returns " WORD(x,7)
say "WORDINDEX(x,3) returns " WORDINDEX(x,3)
say "WORDINDEX(x,10) returns " WORDINDEX(x,10)
say "WORDLENGTH(x,4) returns " WORDLENGTH(x,4)
pause
say 'x = ' x
say "WORDPOS('fox jumps',x) returns " WORDPOS('fox jumps',x)
say "WORDPOS('fox jumps',x,6) returns:"
say WORDPOS('fox jumps',x,6)
say "WORDS(x) returns " WORDS(x)
say "The following are parsing examples"
say "parse SOURCE example"
parse SOURCE word1 word2 word3 word4
say "Saying parsed words"
say word1
say word2
say word3
say word4
say "Parsing the words in x"
parse VAR x word1 word2 word3 word4 word5 word6
say "saying the first 6 words in x"
say word1
say word2
say word3
say word4
say word5
say word6
pause
say "parse version returns:"
parse version word1 word2 word3 word4 word5
say "Saying parsed words"
say word1
say word2
say word3
say word4
say word5
say "Parsing the date() function"
parse value date() with day  month  year
say "Year is " year
say "Month is " month
say "Day is " day
say "Parsing with a template"
y = 'Parse w/ template, use literals - see: Powerful! Yes?
(No)'
parse VAR y var1 '/' var2 ',' var3 '-' var4 ':' var5 '!'
var6 '?' var7
say "Showing variables"
say var1
say var2
say var3
say var4
say var5
say var6
say var7 

Additional query words: 2.10 2.1 2.10a 2.1a 2.20 2.2

Keywords :
Version : :2.1,2.1a,2.2
Platform :
Issue type :


Last Reviewed: November 5, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.