How to use strings in Rexx

Overview

In this shot, we’ll learn how to define a string and discuss the common methods used on a string in Rexx.

Syntax

To define a string, assign a string text to a variable name.

a = "We are learning rexx - strings”
say a

Output

We are learning rexx - strings

Methods

Here some common methods used on a string in Rexx.

1. length(str)

You can use the length method to count the number of characters in a string.

  • Parameters: A string or a reference variable.

  • Return value: An integer that represents the number of characters in the string.

2. reverse(str)

You can use the reverse() method to return a reversed string.

  • Parameters: A string or a reference variable.

  • Return value: A string that is the reverse of the original string.

3. compare(str1,str2)

You can use the compare() method to compare two strings.

  • Parameters:

    • str1 : A string or reference variable.
    • str2 : Another string to compare str1 with.
  • Return value:

    • 0: The strings are equal.
    • Integer: The index of the first character that does not match between the strings.

Code

a = "Hello World"
say length(a) /* 11 */
say reverse(a) /* dlroW olleH */
say compare(a, "Hello World") /* 0 */

The output of the code is as follows:

11

dlroW olleH

0

11 is the length of the string and dlroW olleH is the reverse of the string. 0 means that the strings are equal.

Free Resources