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
- String Variables: How to define and use string variables in CL.
- Concatenation: Combining multiple strings into one.
- Substring Extraction: Extracting a part of a string.
- String Length: Determining the length of a string.
- Searching: Finding a substring within a string.
- 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:
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
&FULLNAMEwill beJohn 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
&FIRSTNAMEwill beJohn.
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
&LENGTHwill be8.
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 substringDoewithin&FULLNAME.- The result in
&POSITIONwill be6.
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
&NEWNAMEwill beJohn Smith.
Practical Exercise
Exercise 1: Concatenate and Extract
- Declare two string variables
&FIRSTNAMEand&LASTNAMEwith lengths of 20. - Declare a string variable
&FULLNAMEwith a length of 40. - Assign values
Aliceto&FIRSTNAMEandJohnsonto&LASTNAME. - Concatenate
&FIRSTNAMEand&LASTNAMEinto&FULLNAME. - Extract the first name from
&FULLNAMEand 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))&EXTRACTEDNAMEwill containAlice.
Exercise 2: Search and Replace
- Declare a string variable
&TEXTwith a length of 50. - Assign the value
Hello Worldto&TEXT. - Search for the substring
Worldin&TEXT. - Replace
WorldwithCL.
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 ')&TEXTwill containHello 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.
CL (Control Language) Course
Module 1: Introduction to CL
- What is Control Language?
- Setting Up Your Environment
- Basic Syntax and Structure
- Writing Your First CL Program
Module 2: Basic CL Commands
- Introduction to CL Commands
- File Management Commands
- Job Management Commands
- System Management Commands
Module 3: Variables and Expressions
Module 4: Control Structures
Module 5: Advanced CL Commands
- Advanced File Operations
- Advanced Job Scheduling
- System Configuration Commands
- Security and Permissions
