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


11.4 Extracting substrings

Substrings are extracted with substr:

Builtin: substr (string, from, [length])

Expands to the substring of string, which starts at index from, and extends for length characters, or to the end of string, if length is omitted. The starting index of a string is always 0. The expansion is empty if there is an error parsing from or length, if from is beyond the end of string, or if length is negative.

The macro substr is recognized only with parameters.

substr(`gnus, gnats, and armadillos', `6')
⇒gnats, and armadillos
substr(`gnus, gnats, and armadillos', `6', `5')
⇒gnats

Omitting from evokes a warning, but still produces output.

substr(`abc')
error→m4:stdin:1: Warning: too few arguments to builtin `substr'
⇒abc
substr(`abc',)
error→m4:stdin:2: empty string treated as 0 in builtin `substr'
⇒abc