You are on page 1of 2

STRSTR Function

The STRSTR function finds a substring inside a string. This function returns the position
of the first instance of the specified substring within the specified string.
If STRSTR does not find the specified substring in the string, it returns a value of
-1.
This function is zero based.
Syntax:
integer = strstr("string","substring");
where: integer
= integer variable
string
= string
substring = part of the string
Example 1:
integer d;
d = strstr("mississippi","is");
//Finds the first instance of the substring "is" inside the string
//"mississippi" and returns the position of that first substring.
// d will equal 1 after execution because this function is zero-based
Example 2:
Given the below input string value, a rule needs to be written to identify what position
the letter B occurs in.
DateofBirth[1^100]
INTEGER POS;
//integer variable to store the position in numeric form
POS = strstr(#FieldA , B);//#FieldA refers to the string element/field in
// the map where the data value resides
After the rules executes, the value returned by the STRSTR function and stored variable
POS will be 6.
The map can then be designed to perform a specific set of instructions based off the
return value of the STRSTR function.
When testing, the rule could assign the integer variable POS to an integer field which is
linked to the output side.

Example 3:
Given the below segment, a rule needs to be written for the fourth element value to
obtain the remaining string value after the / character.
SL1*CX***0318 /H1***TP**I~
INTEGER POS;
INTEGER LENGTH;
POS = 0;
LENGTH = 0;
LENGTH = LEN(#0320);
//#0320 is the field containing the data
POS = STRSTR(#0320,"/");
#tempscale = RIGHT(#0320,(LENGTH - (POS+1)));
The (LENGTH - (POS+1)) specifies the position directly after the slash.
The LENGTH variable will contain the value 10 after the LEN command executes.
The POS variable will contain the value 7 after the STRSTR command executes
because this function is zero based. The (POS + 1) increments the returned value by
one to account for this.
Considerations
If the substring is not found by the extended rule a value of -1 is returned. Extended
rules that use the STRSTR function should be written to check the return value to
verify the substring was found.
DateofBirth[1^100]
INTEGER POS;
//integer variable to store the position in numeric form
POS = strstr(#FieldA , B);//#FieldA refers to the string element/field in
// the map where the data value resides
IF POS != -1 THEN
//substring was found
BEGIN
<perform specific instructions>
END
ELSE
//substring was not found
<perform specific instructions>
If the caret (^) character is used in the data, and the rule needs to return the position
of this character, the hex value of this character must be used.
o The caret character is a reserved character in the extended rule language and is
used to denote that the following sequence of characters is a hex value.
POS = strstr(#FieldA , ^5E); //The 5E is the Hex value of the caret character

You might also like