substr - extract substring from string


<substr $str $offset $len [$mode]>

For every $str value, the substr function returns the substring starting at character $offset of length $len characters, for every $offset and corresponding $len value. If there are fewer values of $len than $offset, the last value of $len is re-used. Indexes start at 0 for the first character. If $offset is negative, then it applies from the end of the string instead of the start. If $len has no or negative values, the length of the rest of the string is assumed.

The optional $mode argument is a stringcomparemode-style (here) compare mode to use; the default is the current apicp stringcomparemode. The $mode values are used in the same order as $len values. Currently the only pertinent $mode flag is "iso-8859-1", which determines whether to interpret text as ISO-8859-1 or UTF-8. This can alter how many raw bytes the character-based $offset and $len arguments cover, as UTF-8 characters are variable-byte-sized. The $mode argument was added in version 6.

substr returns a list of S*O substrings of $str (S and O being the number of values of $str and $offset). Uses stringcomparemode $mode.


<$off = -3 0  0>
<$len =  3 3 -1>
<substr "father" $off $len>

The return values in $ret would be "her", "fat", "father".

Offsets and lengths count characters, not bytes. For byte counting, set $mode to "iso-8859-1".


Copyright © Thunderstone Software     Last updated: Oct 24 2023
Copyright © 2023 Thunderstone Software LLC. All rights reserved.