Google

NetRexx Overview, version 2.02
Copyright (c) IBM Corporation, 2001. All rights reserved. ©
22 May 2001
[previous | contents | next]

Doing things with strings

A character string is the fundamental datatype of NetRexx, and so, as you might expect, NetRexx provides many useful routines for manipulating strings. These are based on the functions of Rexx, but use a syntax that is more like Java or other similar languages:

  phrase='Now is the time for a party'

  say phrase.word(7).pos('r')

The second line here can be read from left to right as:
take the variable phrase, find the seventh word, and then find the position of the first ‘r’ in that word.
This would display ‘3’ in this case, because ‘r’ is the third character in ‘party’.

(In Rexx, the second line above would have been written using nested function calls:


  say pos('r', word(phrase, 7))

which is not as easy to read; you have to follow the nesting and then backtrack from right to left to work out exactly what's going on.)

In the NetRexx syntax, at each point in the sequence of operations some routine is acting on the result of what has gone before. These routines are called methods, to make the distinction from functions (which act in isolation). NetRexx provides (as methods) most of the functions that were evolved for Rexx, including:

  • changestr (change all occurrences of a substring to another)
  • copies (make multiple copies of a string)
  • lastpos (find rightmost occurrence)
  • left and right (return leftmost/rightmost character(s))
  • pos and wordpos (find the position of string or a word in a string)
  • reverse (swap end-to-end)
  • space (pad between words with fixed spacing)
  • strip (remove leading and/or trailing white space)
  • verify (check the contents of a string for selected characters)
  • word, wordindex, wordlength, and words (work with words).

These and the others like them, and the parsing described in the next section, make it especially easy to process text with NetRexx.


[previous | contents | next]

From The NetRexx Language by Mike Cowlishaw, mfc@uk.ibm.com (ISBN 0-13-806332-X, 197pp, Prentice-Hall, 1997).