String Handling Functions
In this article, let’s see about the character string handling functions and how to use string functions in PL/I Programming.
SUBSTR:
Used to extract the required portion of the Character String, Bit or Pic Attributes, by specifying the starting position and the length of the required string.
Syntax:
- Name of the data item = SUBSTR (Identifier, m,n)
- Arg. Identifier denotes on which the SUBSTR function is to be applied.
- Arg. m used to mention the starting position of the SUBSTR operation on the Id.
- Arg. n used to mention the Length of the resulting character.
Examples :
TODAY=DATE;
YEAR=SUBSTR(TODAY,1,4);
MONTH=SUBSTR(TODAY,5,2);
DAY=SUBSTR(TODAY,7,2);
EDITED_TODAY=DAY|| ‘-’ || MONTH || ‘-’|| YEAR;
DCL MESSAGE CHAR(40);
GET LIST(SUBSTR(MESSAGE,20,10));
BIT function in PL/I
Converts a coded arithmetic data item or character string to a bit string. Arg. may be a single value, an Exp. of an array name and the length of resulting bit-string. The second arg. is optional.
Syntax :
DCL Data item1, …, Data item n BIT(identifier)
Examples :
DCL X FIXED BIN(31);
NO_BITS = BIT(X); /* The result will be Bit string 32 */
NO_BITS=BIT(X,15); /* The result will be Bit string 15 */
CHAR
Converts the specified value into Character String. Like BIT, we can use two arguments also. First Arg. is the identifier to be converted and second argument is a decimal constant, indicating the length of the result.
Important note is CHAR can’t be used on POINTER Variables.
Syntax :
DCL Data item1, …, Data item n CHAR(identifier);
Examples :
DCL A FIXED DEC (5) INIT(175);
DCL B FIXED DEC (10) INIT (16500);
DCL C CHAR(5);
C=CHAR(A); /* The result will be ‘bb175’ */
C=CHAR(B); /* The result will be ‘16500’ */
LENGTH
Used to find the length of the given bit or a character string and returns a result as FIXED BINARY form.
Syntax :
Name of the data item = LENGTH(identifier);
Examples :
DCL A CHAR(40);
DCL B FIXED BIN (20);
A= ‘ANAND KANDHASWAMY BALASUBRAMANIAN’;
C=LENGTH(A); /* The result will be 33 */
INDEX in PL/I Programming
Used to search a string for a specified bit or character configuration. The argument may be bit string, character string, binary coded arithmetic, decimal picture or array names. The results returned has the attributes BINARY FIXED(15).
Syntax :
Name of the data item = INDEX(the string to be searched, a bit/char is searched)
Examples :
X = INDEX(SEN,’XX’);
Note:
- If neither arg is bit string or only one arg is bit string, both arg are converted in character string.
- If both arguments are bit strings, no conversion is performed.
- Binary coded arithmetic strings are converted to bit-string.
- Decimal picture arguments are converted to char-string, before the above conversion made.
REPEAT :
This function takes a given string value and forms a new string consisting of the string value concatenated with itself a specified number of times.
Syntax:
REPEAT (decimal integer, char / bit string)
Examples:
NAME=(2) ‘ANAND’;
NAME=REPEAT ( ‘ANAND’, 2); /* ANANDANAND */
TEST_DATA = ‘101’B;
X = REPEAT(TEST_DATE,3); /* ‘101101101’B */
TRANSLATE:
Used to convert a character or a set of character to a character or a set of character in a specified character string.
Syntax :
- Name of the data item = TRANSLATE ( Arg1, Arg2, Arg3);
- Arg1 is the Source Character string,
- Arg2 is the Replacement string,
- Arg3 is the Current or Position string.
Examples :
DCL A CHAR(10),
B CHAR(10),
C CHAR(1),
D CHAR(1);
C = ‘0’;
D = ‘X’;
A = ‘XX123XX’;
B = TRANSLATE(A,C,D); /* The result will be 0012300 */
( Replace all X by 0)
VERIFY function in PL/1
Used to examine Two character strings with each other. It verifies each Character / Bit in the 1st String with the character / Bit of the Second string, returning a fixed binary value of 0 anything found. Otherwise the value returned is the first character in the first string that is not represented in the second string.