Microsoft KB Archive/99066

= Using REXX String Manipulation Commands =

Article ID: 99066

Article Last Modified on 9/30/2003

-

APPLIES TO


 * Microsoft LAN Manager 2.1 Standard Edition
 * Microsoft LAN Manager 2.1a
 * Microsoft LAN Manager 2.2 Standard Edition

-



This article was previously published under Q99066



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 &quot;The REXX Language, A Practical Approach to Programming&quot; 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 &quot;length&quot; 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 &quot;pad&quot; is compared to the remaining part of  the longer string. The default for &quot;pad&quot; is space. }

COPIES(source_string, number)

{ Creates a new string of &quot;number&quot; 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 &quot;string&quot; 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 &quot;length&quot; of the source into the target at the position specified by &quot;start&quot; 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 &quot;length&quot; 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. &quot;template&quot; 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 &quot;string&quot; up to &quot;length&quot;. &quot;pad&quot; will be used to prepend to string if it is shorter than &quot;length&quot;. }

SPACE(string, n, pad_character)

{ Isolates words in a string and returns a reconstructed string containing the words (in the same order) separated by &quot;n&quot; spaces or  pad_characters. &quot;n&quot; 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 &quot;string&quot; beginning at start_position which is &quot;length&quot; long, &quot;length&quot; is either satisfied from &quot;string&quot; itself or by padding the result with &quot;pad_with_this&quot;. }

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(&quot;This is a test&quot;, &quot;123&quot;, &quot;iae&quot;) would produce &quot;Th1s 1s 2 t3st&quot;. }

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

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.