Use the &SYSINDEX built-in function to locate the position
where one character string begins within another character string.
In other words, &SYSINDEX returns the numeric index (or offset)
of string_1 within string_2.
If SYSINDEX does not find string_1 within string_2, &SYSINDEX
returns a value of zero.
Use the following syntax:
&SYSINDEX(string_1,string_2[,start])
where:
- string_1
- is the character string that you are searching for.
- string_2
- is the character string to be searched in.
- start
- is a numeric expression indicating where in string_2 the
search for string_1 should begin. If omitted
or zero, this value defaults to one.
In examples 1-4, assume that &X is DOG, &Y
is CATDOGSDOG and &Z is 2:
1. SET A = &SYSINDEX(&X,&Y) /* result: A = 4
- &SYSINDEX found DOG in the fourth position of CATDOGSDOG,
thus the index is 4.
2. SET A = &SYSINDEX(&X,&Y,&Z) /* result: A = 4
- &SYSINDEX started searching at the second position, and found
DOG again at the fourth position.
3. SET A = &SYSINDEX(&X,&Y,3+&Z) /* result: A = 8
- Because the search started in the fifth position (3+2) &SYSINDEX
found the second occurrence of DOG, in the eighth position.
4. SET A = &SYSINDEX(&X,&Y,9) /* result: A = 0
- The search started in the ninth position and &SYSINDEX can
not find the target string DOG.
Blanks are valid in
string_1 and
string_2.
For example:
SET A = &SYSINDEX(is full,the car is full) /* result: A = 9
To search for a blank in
string_2, you
can set
string_1 to a variable containing
the value &STR( ). For example:
SET BLANK = &STR( )
SET TARG = THIS IS A TEST
SET LOC = &SYSINDEX(&BLANK,&TARG) /* result: &LOC = 5
If
string_1 or
string_2 might
contain a comma or right parenthesis, first set the string to a variable's
value using &STR, then use the variable in &SYSINDEX, again
enclosed in &STR(…). For example:
SET ARG = &STR(,)
SET TARG = &STR((80,60))
SET &LOC = &SYSINDEX(&STR(&ARG),&STR(&TARG)) /* result: &LOC = 4
SET ARG = &STR())
SET TARG = &STR((80,60))
SET &LOC = &SYSINDEX(&STR(&ARG),&STR(&TARG)) /* result: &LOC = 7