String Functions
Returning Strings
• num2str(z)—Converts a scalar z into a string.
• vec2str(v)—Converts a vector of UNICODE codes in v to a string. This function also works with zero-length strings, such as vec2str(0) = "".
• concat("S1", "S2", "S3", ...)—Returns the string formed by concatenating strings S1, S2, S3, ... .
• substr("S", m, n)—Returns the substring of S having n characters starting at position m.
• format("Sf", x1, x2, x3, ...)—Returns a string containing the value of the arguments x1, x2, x3,... with print order and surrounding text specified by Sf. Each argument is referenced as {#}, starting with 0:
format("The date is {0}/{1}/{2}", m, d, y)
You can use the format function in place of concat when you need to make a string out of multiple values of different types.
• error("S")—Returns string
S as an error message. This function is used primarily with programs. You can use the
error function in conjunction with the
if function.
The error function does not support the new-line command /n inside user-defined error messages.
Working with Strings
• str2num("Sn")—Converts a string Sn into a constant.
• str2vec("S")—Converts a string S to a vector of UNICODE codes corresponding to the characters in S. This function also works with zero-length strings, such as str2vec("") = 0
• strlen("S")—Returns the integer number of characters in S.
• search("S", "SubS", m)—Returns the starting position of the substring SubS in S, beginning from position m in S. If the substring is not found after the specified starting position, it returns either −1 or ORIGIN − 1, depending on whether strings are indexed to ORIGIN or not.
Watch this video to learn more about string functions:
Arguments
• S, S1, S2, S3, SubS... are strings.
• Sn is a string representing a number. The number may be complex, in engineering notation, or in decimal format.
• Sf (optional for the one-variable case) is a string containing control codes to specify location of the formatted arguments in the output.
• x1, x2, x3, ... are any valid expressions, including scalars, arrays.
• m, n are nonnegative integers.
• z is a scalar.
• v is a vector of integers representing UNICODE code points for any valid string character. Acceptable values are integers in the range 9, 10, 13, or 32 - 255.
Additional Information
• The position of the first character in a string is zero, unless you decide to associate string indexing with the array ORIGIN for the worksheet. For example, in the string "yes" the letter "y" is the 0th character.
• The mapping of codes to characters is done by your system codepage, and depends on the language of your operating system. It is possible for the same document to produce different strings/codes depending on the currently installed codepage of the system on which it is run.