Documentation Center

  • Trial Software
  • Product Updates

stringlib::subs

Substitution in a string

Use only in the MuPAD Notebook Interface.

This functionality does not run in MATLAB.

Syntax

stringlib::subs(string, substring = replacement, <First>)

Description

stringlib::subs substitutes a substring by another string.

By default, every occurrence of the string substring in string is replaced by replacement. The option First causes only the first appearance of substring to be replaced.

The result is not searched again for instances of substring. See Example 3.

Among several overlapping occurrences of substring, the leftmost one is replaced.

Examples

Example 1

The string replacement may be empty.

stringlib::subs("abcdeabcdeabcde", "bc" = "")

Example 2

Every substring is replaced unless the option First is given.

stringlib::subs("abcdeabcdeabcde", "bc" = "xxx")

stringlib::subs("abcdeabcdeabcde", "bc" = "xxx", First)

Example 3

The substitution may produce a new instance of substring, but this one is not replaced.

stringlib::subs("aab", "ab"="b")

Example 4

Collapse all whitespace in strings (see stringlib::collapseWhitespace):

f := proc(x) local y; begin y := 2*x; x + y end_proc:
string := expr2text(f)

string := stringlib::subs(string, "\n" = " "):
string := stringlib::subs(string, "  " = " "):
string := stringlib::subs(string, "  " = " ")

Parameters

string

Non empty string

substring

Non empty string that should be replaced

replacement

Any string that replaced substring

Options

First

Determines that only the first appearance of substring in string will be replaced

Return Values

Given string with substring replaced by replacement

See Also

MuPAD Functions

Was this topic helpful?