strstr

The strstr function finds a substring inside a string. This function returns the position of the first instance of the designated substring within the specified string. If this function does not find the specified substring in the string, it returns a value of -1. This function is zero-based.

Common use

The strstr function is often used with the mid function to return a substring. For example, if you wanted to extract a 10-digit PO number that is listed after a slash, use the following example:

String[10] po_number;
po_number = mid(#PONUM,strstr(#PONUM,“/”),10);

If you do not know the length of the substring, the strstr function can also be used to determine how many characters are to the right of the character, by subtracting the position returned by strstr from the length using len.

String[20] po_number;
po_number = mid(#PONUM,strstr(#PONUM,“/”),(len(#PONUM)–strstr(#PONUM,“/”)));

Syntax

integer = strstr("string","substring");

where:

  • integer = integer variable
  • string = the string to evaluate
  • substring = the part of the string you are interested in

Examples

integer d;
d = 0;
d = strstr("mississippi","is");
//Finds the first instance of the substring "is" inside the string
//"mississippi" and returns the position of that first substring.

To use this function to enable a purchase order number to be processed differently depending on its format (for example, if the third position of the purchase order number is numeric do "X," otherwise do "Y"), use the following example:

integer position;
string [1] PONumChar2;
PONumChar2=mid(#PONumber, 1, 1);
position=strstr("0123456789", PONumChar2);

//This function finds a substring within the string. So if the second 
//position of purchase order number is not equal to -1 then it is 
//numeric and "X" should be executed. Otherwise, the second position is 
//not numeric and "Y" should be executed.