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
- String Concatenation
- Substring Extraction
- String Functions
LENGTH
SUBSTR
POS
LEFT
andRIGHT
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 Functions
LENGTH
The LENGTH
function returns the length of a string.
Example:
SUBSTR
The SUBSTR
function extracts a substring from a string.
Example:
POS
The POS
function returns the position of a substring within a string. If the substring is not found, it returns 0.
Example:
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:
Exercise 3: Find Position
Write a REXX program to find the position of the word "fun" in the string "REXX is fun".
Solution:
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.
REXX Programming Course
Module 1: Introduction to REXX
- What is REXX?
- Setting Up the REXX Environment
- Hello World in REXX
- Basic Syntax and Structure
- Variables and Data Types
Module 2: Basic Programming Concepts
- Operators and Expressions
- Control Structures: IF/THEN/ELSE
- Loops: DO and LEAVE
- Input and Output
- Basic String Manipulation
Module 3: Intermediate REXX Programming
Module 4: Advanced REXX Programming
- Advanced String Manipulation
- Parsing Techniques
- Interfacing with External Programs
- REXX Macros
- Performance Optimization