strnpart/4—determine the start position of a specified occurrence of a part of a string
The strnpart/4 function determines the start position of a specified occurrence of a part of a string. The syntax for this function is as follows:
$POS=strnpart($STR,$PART,$CNT)
strnpart/4 arguments
Argument | Mode | Type | Description |
---|---|---|---|
$STR | Input | STRING | Specifies the base string within which you want to determine the starting position of a specified re-occurrence of another partial string |
$PART | Input | STRING | Specifies the partial string for which you are trying to determine the starting position after a specified number of occurrences within $STR |
$CNT | Input | INTEGER | Specifies the number of occurrences of the partial string to count before returning the position of the beginning of the last counted occurrence of the partial string |
$POS | Output | INTEGER | Starting position of the partial string within $STR |
Use the strnpart/4 function to determine at what position in $STR the number of occurrences for the partial string $PART reaches $CNT number of occurrences. The position is returned in $POS.
The first character of the string is counted as 1. If the partial string does not occur at least $CNT times in the base string, the value 0 is returned as position.
strnpart/4 example
$ZPOS = strnpart($E.mc_host,'.',2);
if ( $HLEN == 0 OR $ZPOS == 0 OR $ZPOS == $HLEN ) then
$E.mc_location = "Unknown";
else
$E.mc_location = substring($E.mc_host,$ZPOS,$HLEN-$ZPOS);