Next: , Previous: , Up: Text handling   [Contents][Index]

11.2 Searching for substrings

Searching for substrings is done with index:

Builtin: index (string, substring)

Expands to the index of the first occurrence of substring in string. The first character in string has index 0. If substring does not occur in string, index expands to ‘-1’.

The macro index is recognized only with parameters.

index(`gnus, gnats, and armadillos', `nat')
index(`gnus, gnats, and armadillos', `dag')

Omitting substring evokes a warning, but still produces output; contrast this with an empty substring.

error→m4:stdin:1: Warning: too few arguments to builtin `index'
index(`abc', `')
index(`abc', `b')