String manipulation is a fundamental aspect of programming, and REXX provides a variety of built-in functions to handle strings efficiently. In this section, we will cover the basics of string manipulation, including concatenation, substring extraction, and common string functions.

Key Concepts

  1. String Concatenation
  2. Substring Extraction
  3. String Functions
    • LENGTH
    • SUBSTR
    • POS
    • LEFT and RIGHT
    • OVERLAY
    • STRIP
    • TRANSLATE

String Concatenation

In REXX, you can concatenate strings using the || operator or simply by placing strings next to each other with a space in between.

Example:

/* Using || operator */
string1 = "Hello"
string2 = "World"
result = string1 || " " || string2
say result  /* Output: Hello World */

/* Using space */
result = string1 " " string2
say result  /* Output: Hello World */

Substring Extraction

You can extract a substring from a string using the SUBSTR function. The syntax is SUBSTR(string, start, length).

Example:

string = "Hello World"
substring = SUBSTR(string, 7, 5)
say substring  /* Output: World */

String Functions

LENGTH

The LENGTH function returns the length of a string.

Example:

string = "Hello"
length = LENGTH(string)
say length  /* Output: 5 */

SUBSTR

The SUBSTR function extracts a substring from a string.

Example:

string = "Hello World"
substring = SUBSTR(string, 1, 5)
say substring  /* Output: Hello */

POS

The POS function returns the position of a substring within a string. If the substring is not found, it returns 0.

Example:

string = "Hello World"
position = POS("World", string)
say position  /* Output: 7 */

LEFT and RIGHT

The LEFT and RIGHT functions return a specified number of characters from the left or right side of a string, respectively.

Example:

string = "Hello World"
leftPart = LEFT(string, 5)
rightPart = RIGHT(string, 5)
say leftPart  /* Output: Hello */
say rightPart  /* Output: World */

OVERLAY

The OVERLAY function replaces part of a string with another string.

Example:

string = "Hello World"
newString = OVERLAY("REXX", string, 7)
say newString  /* Output: Hello REXX */

STRIP

The STRIP function removes leading and/or trailing spaces from a string.

Example:

string = "  Hello World  "
strippedString = STRIP(string)
say strippedString  /* Output: Hello World */

TRANSLATE

The TRANSLATE function converts characters in a string to uppercase or lowercase.

Example:

string = "Hello World"
upperString = TRANSLATE(string)
lowerString = TRANSLATE(string, "abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
say upperString  /* Output: HELLO WORLD */
say lowerString  /* Output: hello world */

Practical Exercises

Exercise 1: Concatenate Strings

Write a REXX program that concatenates three strings: "REXX", "is", "fun".

Solution:

string1 = "REXX"
string2 = "is"
string3 = "fun"
result = string1 || " " || string2 || " " || string3
say result  /* Output: REXX is fun */

Exercise 2: Extract Substring

Write a REXX program to extract the word "World" from the string "Hello World".

Solution:

string = "Hello World"
substring = SUBSTR(string, 7, 5)
say substring  /* Output: World */

Exercise 3: Find Position

Write a REXX program to find the position of the word "fun" in the string "REXX is fun".

Solution:

string = "REXX is fun"
position = POS("fun", string)
say position  /* Output: 9 */

Exercise 4: Replace Substring

Write a REXX program to replace the word "World" with "REXX" in the string "Hello World".

Solution:

string = "Hello World"
newString = OVERLAY("REXX", string, 7)
say newString  /* Output: Hello REXX */

Common Mistakes and Tips

  • Off-by-One Errors: Remember that string positions in REXX start at 1, not 0.
  • Case Sensitivity: REXX string functions are case-sensitive. Use TRANSLATE to handle case conversions if needed.
  • Whitespace Handling: Use STRIP to remove unwanted leading or trailing spaces from strings.

Conclusion

In this section, we covered the basics of string manipulation in REXX, including concatenation, substring extraction, and various string functions. These tools are essential for handling and processing text data in your REXX programs. In the next module, we will delve into more advanced programming concepts, such as functions and subroutines.

© Copyright 2024. All rights reserved