Extract a substring from a string

MuPAD® notebooks will be removed in a future release. Use MATLAB® live scripts instead.

MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.


substring(string, i)
substring(string, i, l)
substring(string, i .. j)


substring(string, i) returns the i-th character of a string.

substring(string, i, l) returns the substring of length l starting with the i-th character of the string.

substring(string, i..j) returns the substring consisting of the characters i through j, inclusive.

The empty string "" is returned if the length l = 0 is specified.

substring is considered obsolete. You should use index access to strings instead.


Example 1

We extract individual characters from a string:

substring("123456789", i) $ i = 1..9

Substrings of various lengths are extracted:

substring("123456789", 1, 2), substring("123456789", 4, 4)

Substrings of length 0 are empty strings:

substring("123456789", 4, 0)

Ranges may be used to specify the substrings:

substring("123456789", 1..9)

Example 2

The following while loop removes all trailing blank characters from a string:

string := "MuPAD       ":
while substring(string, length(string)) = " " do
  string := substring(string, 1..length(string) - 1)



A nonempty character string


An integer between 1 and length(string)


An integer between 1 and length(string)


An integer between i and length(string)

Return Values

Character string

See Also

MuPAD Functions