String manipulation is a fundamental aspect of programming in Control Language (CL). This module will cover the basics of handling and manipulating strings, including common operations such as concatenation, extraction, and searching within strings.

Key Concepts

  1. String Variables: How to define and use string variables in CL.
  2. Concatenation: Combining multiple strings into one.
  3. Substring Extraction: Extracting a part of a string.
  4. String Length: Determining the length of a string.
  5. Searching: Finding a substring within a string.
  6. Replacing: Replacing parts of a string with another string.

String Variables

In CL, strings are typically defined using the DCL (Declare) command. Here’s how you can declare a string variable:

DCL VAR(&MYSTRING) TYPE(*CHAR) LEN(50)
  • VAR(&MYSTRING): The name of the variable.
  • TYPE(*CHAR): Specifies that the variable is a character string.
  • LEN(50): The length of the string.

Concatenation

Concatenation is the process of joining two or more strings together. In CL, you can use the CHGVAR (Change Variable) command to concatenate strings.

DCL VAR(&FIRSTNAME) TYPE(*CHAR) LEN(20)
DCL VAR(&LASTNAME) TYPE(*CHAR) LEN(20)
DCL VAR(&FULLNAME) TYPE(*CHAR) LEN(40)

CHGVAR VAR(&FIRSTNAME) VALUE('John')
CHGVAR VAR(&LASTNAME) VALUE('Doe')
CHGVAR VAR(&FULLNAME) VALUE(&FIRSTNAME *CAT ' ' *CAT &LASTNAME)
  • *CAT: Concatenation operator.
  • The result in &FULLNAME will be John Doe.

Substring Extraction

To extract a substring from a string, you can use the %SST (Substring) built-in function.

DCL VAR(&FULLNAME) TYPE(*CHAR) LEN(40)
DCL VAR(&FIRSTNAME) TYPE(*CHAR) LEN(20)

CHGVAR VAR(&FULLNAME) VALUE('John Doe')
CHGVAR VAR(&FIRSTNAME) VALUE(%SST(&FULLNAME 1 4))
  • %SST(&FULLNAME 1 4): Extracts a substring starting at position 1 with a length of 4 characters.
  • The result in &FIRSTNAME will be John.

String Length

To determine the length of a string, you can use the %LEN built-in function.

DCL VAR(&FULLNAME) TYPE(*CHAR) LEN(40)
DCL VAR(&LENGTH) TYPE(*DEC) LEN(5 0)

CHGVAR VAR(&FULLNAME) VALUE('John Doe')
CHGVAR VAR(&LENGTH) VALUE(%LEN(&FULLNAME))
  • %LEN(&FULLNAME): Returns the length of the string &FULLNAME.
  • The result in &LENGTH will be 8.

Searching

To find a substring within a string, you can use the %SCAN built-in function.

DCL VAR(&FULLNAME) TYPE(*CHAR) LEN(40)
DCL VAR(&POSITION) TYPE(*DEC) LEN(5 0)

CHGVAR VAR(&FULLNAME) VALUE('John Doe')
CHGVAR VAR(&POSITION) VALUE(%SCAN('Doe' &FULLNAME))
  • %SCAN('Doe' &FULLNAME): Searches for the substring Doe within &FULLNAME.
  • The result in &POSITION will be 6.

Replacing

To replace parts of a string with another string, you can use a combination of %SST and CHGVAR.

DCL VAR(&FULLNAME) TYPE(*CHAR) LEN(40)
DCL VAR(&NEWNAME) TYPE(*CHAR) LEN(40)

CHGVAR VAR(&FULLNAME) VALUE('John Doe')
CHGVAR VAR(&NEWNAME) VALUE(&FULLNAME)
CHGVAR VAR(%SST(&NEWNAME 6 3)) VALUE('Smith')
  • %SST(&NEWNAME 6 3): Targets the substring starting at position 6 with a length of 3 characters.
  • The result in &NEWNAME will be John Smith.

Practical Exercise

Exercise 1: Concatenate and Extract

  1. Declare two string variables &FIRSTNAME and &LASTNAME with lengths of 20.
  2. Declare a string variable &FULLNAME with a length of 40.
  3. Assign values Alice to &FIRSTNAME and Johnson to &LASTNAME.
  4. Concatenate &FIRSTNAME and &LASTNAME into &FULLNAME.
  5. Extract the first name from &FULLNAME and store it in a new variable &EXTRACTEDNAME.

Solution

DCL VAR(&FIRSTNAME) TYPE(*CHAR) LEN(20)
DCL VAR(&LASTNAME) TYPE(*CHAR) LEN(20)
DCL VAR(&FULLNAME) TYPE(*CHAR) LEN(40)
DCL VAR(&EXTRACTEDNAME) TYPE(*CHAR) LEN(20)

CHGVAR VAR(&FIRSTNAME) VALUE('Alice')
CHGVAR VAR(&LASTNAME) VALUE('Johnson')
CHGVAR VAR(&FULLNAME) VALUE(&FIRSTNAME *CAT ' ' *CAT &LASTNAME)
CHGVAR VAR(&EXTRACTEDNAME) VALUE(%SST(&FULLNAME 1 5))
  • &EXTRACTEDNAME will contain Alice.

Exercise 2: Search and Replace

  1. Declare a string variable &TEXT with a length of 50.
  2. Assign the value Hello World to &TEXT.
  3. Search for the substring World in &TEXT.
  4. Replace World with CL.

Solution

DCL VAR(&TEXT) TYPE(*CHAR) LEN(50)
DCL VAR(&POSITION) TYPE(*DEC) LEN(5 0)

CHGVAR VAR(&TEXT) VALUE('Hello World')
CHGVAR VAR(&POSITION) VALUE(%SCAN('World' &TEXT))
CHGVAR VAR(%SST(&TEXT &POSITION 5)) VALUE('CL   ')
  • &TEXT will contain Hello CL.

Conclusion

In this module, you learned how to manipulate strings in CL, including concatenation, extraction, determining string length, searching, and replacing substrings. These skills are essential for handling text data and performing various string operations in your CL programs. In the next module, we will delve into control structures, which will allow you to make decisions and repeat actions in your programs.

© Copyright 2024. All rights reserved