Language Reference


SUBSTR Function

SUBSTR (matrix, position <, length> );

The SUBSTR function takes a character matrix as an argument (along with starting positions and lengths) and produces a character matrix with the same dimensions as the argument. Elements of the result matrix are substrings of the corresponding argument elements.

The arguments to the SUBSTR function are as follows:

matrix

is a character matrix or quoted literal.

position

is a numeric matrix or scalar that contains the starting position.

length

is a numeric matrix or scalar that contains the length of the substring.

Each substring is constructed by using the starting position supplied. If a length is supplied, this length is the length of the substring. If no length is supplied, the remainder of the argument string is the substring.

The arguments can be scalars or numeric matrices. If more than one argument is a matrix, all matrix arguments must have the same dimensions. If matrix is a matrix, its dimensions determine the dimensions of the output of the function. If matrix is a scalar, the dimensions of the position or length determine the dimensions of the output of the function.

If length is supplied, the element length of the result is MAX(length); otherwise, the element length of the result is

\[  \mbox{NLENG}(\mbox{matrix}) - \mbox{MIN}(\mbox{position}) + 1  \]

The following statements return the output shown:

m = {abc def ghi, jkl mno pqr};
a = substr(m, 3, 2);
print a;

s = "ABCDE";
b = substr(s, 1:4, 5:2);
print b;

Figure 24.402: Substrings of a Character Matrix and String

a
C F I
L O R

b
ABCDE BCDE CDE DE



In the example output, the element size of matrix a is 2; the elements are padded with blanks. Matrix b is a $1 \times 4$ matrix that contains various substrings of the string s.