Extracts a substring of a string.


y = strsect(str, start[, len])#
  • str (string, scalar, string array, or dataframe where all column types are a string or category.) – data from which the string segment is to be obtained.

  • start (scalar) – the index of the substring in str. The index of the first character is 1. A negative start value will use a start position that many characters from the end of the string.

  • len (scalar) – Optional. The length of the substring. If not provided, will select text to the end of the string.


y (string) – the extracted substring, or a null string if start is greater than the length of str or less than 0.


// String to search in
strng = "This is an example string.";

// Remove section of string
y = strsect(strng, 12, 7);

The above code assigns the variable y to be:



If there are not enough characters in a string for the defined substring to be extracted, then a short string or a null string will be returned.

If str is a matrix containing character data, it must be scalar.

See also

Functions strlen(), strindx(), strrindx(), strreplace()