SEARCH

SEARCH returns an unscaled REAL FIXED BINARY value specifying the first position in one string at which any character, bit, graphic, uchar, or widechar of another string appears. It also allows you to specify the location at which to start searching.

Read syntax diagramSkip visual syntax diagramSEARCH( x, y, n)
x and y
Expressions. x specifies the string in which to search for any character, bit, graphic, uchar, or widechar that appears in string y.

If either x or y are the null string, the result is zero.

If y does not occur in x, the result is zero.

n
Expression. n specifies the location within x at which to begin searching. It must have a computational type and is converted to FIXED BINARY(31,0).

Unless 1 ≤ n ≤ LENGTH(x)+1, STRINGRANGE condition, if enabled, is raised. Its implicit action and normal return give a result of zero.

The BIFPREC compiler option determines the precision of the result returned.

SEARCH can be used to find delimiters in a string of numbers.

SEARCH will perform best when the second and third arguments are either literals, named constants declared with the VALUE attribute, or restricted expressions.

Example 1

  dcl Source char value(' Our PL/I wields the Power ');
  dcl Pos fixed bin(31);

/* Find occurrences of any of the characters 'P','o',or 'w' in source *  /

  Pos = search (Source, 'Pow');              /*  returns 6 for the 'P'    */
  Pos = search (Source, 'Pow', Pos+1);    /*  returns 11 for the 'w'   */
  Pos = search (Source, 'Pow', Pos+1);    /*  returns 22 for the 'P'   */
  Pos = search (Source, 'Pow', Pos+1);    /*  returns 23 for the 'o'   */
  Pos = search (Source, 'Pow', Pos+1);    /*  returns 24 for the 'w'   */

  Pos = index (source, 'Pow',1);             /*  returns 22 for the 'Pow' */

In the above example, SEARCH returns the position at which any of the three characters ('P', 'o', or 'w') appear. INDEX returns the position at which the whole string 'Pow' appears.

Example 2

  dcl Source char value (' 368,475;121.,856,478')
  dcl Delims char(3) init (',;.');             /* string of delimiters */
  dcl Number(5) char(3);
  dcl Start fixed bin(31);
  dcl End fixed bin(31);

  /* Extract the three-digit numbers from the source string */
  /* by searching for the delimiters                        */
  Start = verify (Source, ' ');
                     /* find start of first number */
  End   = search (Source, ',;.', Start );
                     /* find end of first number */
  if End = 0 then
     End = length (Source) + 1;
  Number(1) = substr (Source, Start, 3);        /* 368 */
  Start = verify (Source, Delims, End);
               /* find start of second number */
  End   = search (Source, Delims, Start );
  Number(2) = substr (Source, Start, 3);        /* 475 */