Categories &

Functions List

Class Definition: string

datatypes: string

Array representing sequences of characters.

A string array is an array, where each element stores a sequence of characters of arbitrary length.

A string array can also have missing elements, which differ from a sequence of characters of zero length (the equivalent of an empty character vector).

To enable existing functions to handle string arrays as if they were cell arrays of character vectors or character arrays, use the convertCharsToStrings function inside your code. To enable functions working with string arrays to accept cell arrays of character vectors or character vectors as if they were string arrays or string scalars, use the convertStringsToChars function.

See also: convertCharsToStrings, convertStringsToChars

Source Code: string

Methods

string: str = string ()
string: str = string (in)
string: str = string (calendarDuration, 'Format', FMT)
string: str = string (duration, 'Format', FMT)
string: str = string ({in1, …, inN})

str = string () creates a scalar string array, whose element contains an empty character vector.

str = string (in) creates a string array of the same size as in, which is converted to string according to the following options:

  • character arrays are converted so that each row becomes a string element, with any trailing whitespace preserved; cell arrays of character vectors are stored as-is.
  • numeric arrays are converted via the code num2str function.
  • logical arrays are converted to either false or true character sequences.
  • categorical arrays are converted via their cellstr method.
  • datetime arrays are converted via their dispstrings method.
  • calendarDuration arrays and duration arrays are converted via their respective cellstr methods, in which case an extra pair argument is supported to allow parsing to the respective method the appropriate display format. See calendarDuration and duration for valid formats parsed through FMT to each class method. Extra input arguments to the string constructor except for this case are ignored.
  • missing arrays are converted to a string array of missing elements.

str = string ({in1, …, inN}) creates a string array from a cell array, which may contain any combination of the aforementioned data types, provided that each cell element is compatible to a string scalar. When using this syntax, calendarDuration arrays and duration arrays are converted via their dispstrings method, hence no extra format argument is meaningful.

See also: calendarDuration, categorical, datetime, duration, missing

string: cstr = dispstrings (str)

cstr = dispstrings (str) returns a cellstr array of character vectors, cstr, which has the same size as the input string object, str. These character vectors will either be the string contents of each corresponding element or <missing> for missing values.

Composed string elements, i.e. double quoted strings, are translated so that any special characters are represented by their corresponding escaped character sequence, unless the input string, str, is a scalar, in which case text retains its original composition but newlines are prepadded with four white space characters for aligned display.

string: cstr = cellstr (str)

cstr = cellstr (str) returns a cell array of character vectors, cstr, which has the same size as the input string str. Both empty strings and missing values are returned as empty character vectors.

string: c_arr = cell (str)

c_arr = cell (str) returns a cell array, c_arr, which has the same size as the input string str. All elements in str that represent real or complex numbers are converted to equivalent double values, whereas all other non-missing elements are converted to character vectors. Zero-length strings are converted to '' empty character vectors, while missing values are returned as [] empty numeric vectors.

string: c_mat = char (str)

c_mat = char (str) returns a character matrix, c_mat, which contains as many rows as the elements of the string. Both empty strings and missing values are returned as empty character vectors.

string: X = double (str)

X = char (str) returns a double array, X, which has the same size as the input string str. All elements in str that represent real or complex numbers are converted to equivalent double values. Otherwise, NaN is returned.

string: sz = size (str)
string: dim_sz = size (str, dim)
string: dim_sz = size (str, d1, d2, …)
string: [rows, columns, …, dim_n_sz] = size (…)

sz = size (str) returns a row vector with the size (number of elements) of each dimension for the string array str.

dim_sz = size (str, dim) returns the size of the corresponding dimension specified in dim. If dim is a vector, then dim_sz is a vector of the same length and with each element corresponding to a specified dimension. Multiple dimensions may also be specified as separate arguments.

With a single output argument, size returns a row vector. When called with multiple output arguments, size returns the size of dimension N in the Nth argument.

string: out = ndims (str)

out = ndims (str) returns the number of dimensions of the string array D.

string: out = numel (str)

string: N = length (str)

N = length (str) returns the size of the longest dimension of the string array str, unless any of its dimensions has zero length, in which case length (D) returns 0.

string: out = strlength (str)

string: out = count (str, pattern)
string: out = count (str, pattern, 'IgnoreCase', true)

out = count (str, pattern) returns a numerical array out of the same size as str containing the number of occurences of pattern in each corresponding element of str.

out = count (str, pattern, 'IgnoreCase', true) ignores case when identifying occurences of pattern.

string: hey = keyHash (str)

h = keyHash (str) generates a uint64 scalar that represents the input array str. keyHash utilizes the 64-bit FNV-1a variant of the Fowler-Noll-Vo non-cryptographic hash function.

h = keyHash (str), base also generates a 64-bit hash code using base as the offset basis for the FNV-1a hash algorithm. base must be a uint64 integer type scalar. Use this syntax to cascade keyHash on multiple objects for which a single hash code is required.

Note that unlike MATLAB, this implementation does not use any random seed. As a result, keyHash will always generate the exact same hash key for any particular input across different workers and Octave sessions.

string: TF = contains (str, pattern)
string: TF = contains (str, pattern, 'IgnoreCase', true)

TF = contains (str, pattern) returns a logical array TF of the same size as A containing true for each corresponding element of str that contains the specified pattern and false otherwise. Similarly to NaN values, <missing> elements do not match any pattern and always return false.

TF = contains (str, pattern, 'IgnoreCase', true) ignores case when determining if str ends with pattern.

string: TF = endsWith (str, pattern)
string: TF = endsWith (str, pattern, 'IgnoreCase', true)

TF = endsWith (str, pattern) returns a logical array TF of the same size as A containing true for each corresponding element of str that ends with the specified pattern and false otherwise. Similarly to NaN values, <missing> elements do not match any pattern and always return false.

TF = endsWith (str, pattern, 'IgnoreCase', true) ignores case when determining if str ends with pattern.

string: TF = startsWith (str, pattern)
string: TF = startsWith (str, pattern, 'IgnoreCase', true)

TF = startsWith (str, pattern) returns a logical array TF of the same size as A containing true for each corresponding element of str that starts with the specified pattern and false otherwise. Similarly to NaN values, <missing> elements do not match any pattern and always return false.

TF = startsWith (str, pattern, 'IgnoreCase', true) ignores case when determining if str starts with pattern.

string: TF = matches (str, pattern)
string: TF = matches (str, pattern, 'IgnoreCase', true)

TF = matches (str, pattern) returns a logical array TF of the same size as A containing true for each corresponding element of str that matches the specified pattern and false otherwise. Similarly to NaN values, <missing> elements do not match any pattern and always return false.

TF = matches (str, pattern, 'IgnoreCase', true) ignores case when determining if str starts with pattern.

string: TF = iscolumn (str)

TF is true, if string array str is a column vector.

string: TF = isempty (str)

TF is true, if string array str is empty.

string: TF = ismatrix (str)

TF is true, if string array str is a matrix.

string: TF = ismember (A, B)
string: TF = ismember (A, B, 'rows')
string: [TF, index] = ismember (…)
string: [TF, index] = ismember (…, 'legacy')

TF = ismember (A, B) returns a logical array TF of the same size as A containing true for each corresponding element of A that is in B and false otherwise. Similarly to NaN values, <missing> elements are not equal with each other and always return false.

TF = ismember (A, B, 'rows') only applies to string matrices with the same number of columns, in which case the logical vector TF contains true for each row of A that is also a row in B. TF has the same number of rows as A.

[TF, index] = ismember (A, B) also returns an index array of the same size as A containing the lowest index in B for each element of A that is a member of B and 0 otherwise. If the 'rows' optional argument is used, then the returning index is a column vector with the same rows as A and it contains the lowest index in B for each row of A that is a member of B and 0 otherwise. If the 'legacy' optional argument is specified, then the highest index of matched elements is returned. Unless multiple matches exist, the 'legacy' option has no effect on the returned index.

string: TF = ismissing (str)
string: TF = ismissing (str, indicator)

TF = ismissing (str) returns a logical array, TF, with any true values corresponding to missing elements in the input string array str.

TF = ismissing (str, indicator) also returns a logical array, TF, with any true values corresponding to elements in the input string array str, which are lexicographically equal to the values in indicator.

indicator must be either a character vector or a string vector or a cell vector of character vectors.

The output array TF has the same size as the input array str.

string: TF = isrow (str)

TF is true, if string array str is a row vector.

string: TF = isscalar (str)

TF is true, if string array str is a scalar.

string: TF = issorted (str)
string: TF = issorted (str, dim)
string: TF = issorted (str, direction)
string: TF = issorted (str, dim, direction)
string: TF = issorted (…, 'MissingPlacement', MP)

TF = issorted (str) returns a logical scalar TF, which is true, if the string array str is sorted in ascending order, and false otherwise.

TF = issorted (str, dim) returns a logical scalar TF, which is true, if the string array str is sorted in ascending order along the dimension dim, and false otherwise.

TF = issorted (str, direction) returns a logical scalar TF, which is true, if the string array str is sorted in the direction specified by direction, and false otherwise. direction can be any of the following options:

  • 'ascend', which is the default, checks if elements are in ascending order.
  • 'descend' checks if elements are in descending order.
  • 'monotonic' checks if elements are either in ascending or descending order.
  • 'strictascend' checks if elements are in ascending order and there are no duplicate or missing elements.
  • 'strictdescend' checks if elements are in descending order and there are no duplicate or missing elements.
  • 'strictmonotonic' checks if elements are either in ascending or descending order and there are no duplicate or missing elements.

TF = issorted (…, 'MissingPlacement', MP) specifies where missing elements (<missing>) are placed with one of the following options specified in MP:

  • 'auto', which is the default, places missing elements last for ascending sort and first for descending sort.
  • 'first' places missing elements first.
  • 'last' places missing elements last.
string: TF = issortedrows (str)
string: TF = issortedrows (str, col)
string: TF = issortedrows (str, direction)
string: TF = issortedrows (str, col, direction)
string: TF = issortedrows (…, 'MissingPlacement', MP)

TF = issortedrows (str) returns a logical scalar TF, which is true, if the rows in the 2-D string array str are sorted in ascending order, and false otherwise.

TF = issortedrows (str, col) returns a logical scalar TF, which is true, if the string array str is sorted according to the columns specified by the vector col, and false otherwise. col must explicitly contain non-zero integers whose absolute values index existing columns in str. Positive elements sort the corresponding columns in ascending order, while negative elements sort the corresponding columns in descending order.

TF = issortedrows (str, direction) checks if the rows in str are sorted according to the specified direction, which can be one of the following options:

  • 'ascend', which is the default, checks if elements are in ascending order.
  • 'descend' checks if elements are in descending order.
  • 'monotonic' checks if elements are either in ascending or descending order.
  • 'strictascend' checks if elements are in ascending order and there are no duplicate or missing elements.
  • 'strictdescend' checks if elements are in descending order and there are no duplicate or missing elements.
  • 'strictmonotonic' checks if elements are either in ascending or descending order and there are no duplicate or missing elements.

Alternatively, direction can be a cell array of character vectors specifying the sorting direction for each individual column of str, in which case the number of elements in direction must equal the number of columns in str.

TF = issortedrows (str, col, direction) checks if the rows in the string array str are sorted according to the columns specified in col using the corresponding sorting direction specified in direction. In this case, the sign of the values in col is ignored. col and direction must have the same length, but not necessarily the same number of elements as the columns in str.

TF = issortedrows (…, 'MissingPlacement', MP) specifies where missing elements (<missing>) are placed with one of the following options specified in MP:

  • 'auto', which is the default, places missing elements last for ascending sort and first for descending sort.
  • 'first' places missing elements first.
  • 'last' places missing elements last.
string: TF = isstring (str)

TF is true for string inputs.

string: TF = isvector (str)

TF is true, if string array str is a vector.

string: TF = eq (A, B)

TF is true, if string A is lexicographically equal to B. If one input is a string array, the other input can be a string array, a character vector, or a cell array of character vectors. This is equivalent to the strcmp function.

string: TF = ge (A, B)

TF is true, if string A is lexicographically greater than or equal to B. If one input is a string array, the other input can be a string array, a character vector, or a cell array of character vectors.

string: TF = gt (A, B)

TF is true, if string A is lexicographically greater than B. If one input is a string array, the other input can be a string array, a character vector, or a cell array of character vectors.

string: TF = le (A, B)

TF is true, if string A is lexicographically less than or equal to B. If one input is a string array, the other input can be a string array, a character vector, or a cell array of character vectors.

string: TF = lt (A, B)

TF is true, if string A is lexicographically less than B. If one input is a string array, the other input can be a string array, a character vector, or a cell array of character vectors.

string: TF = ne (A, B)

TF is true, if string A is lexicographically not equal to B. If one input is a string array, the other input can be a string array, a character vector, or a cell array of character vectors. TF = ne (A, B) is equivalent to TF = ! strcmp (A, B).

string: TF = strcmp (A, B)

TF is true, if string A is lexicographically equal to B. If one input is a string array, the other input can be a string array, a character vector, or a cell array of character vectors.

If either A or B is a string array or a cell array of character vectors, then a logical array TF of the same size is returned, containing the values described above for every member of the array. In this case, the other argument may also be a string array or a cell array of character vectors (of the same size or scalar), or a character vector.

string: TF = strcmpi (A, B)

TF is true, if string A is lexicographically equal to B, disregarding case of alphabetic characters. If one input is a string array, the other input can be a string array, a character vector, or a cell array of character vectors.

If either A or B is a string array or a cell array of character vectors, then a logical array TF of the same size is returned, containing the values described above for every member of the array. In this case, the other argument may also be a string array or a cell array of character vectors (of the same size or scalar), or a character vector.

string: TF = strncmp (A, B, n)

TF is true, if the first n characters of strings A and B are lexicographically equal. If one input is a string array, the other input can be a string array, a character vector, or a cell array of character vectors.

If either A or B is a string array or a cell array of character vectors, then a logical array TF of the same size is returned, containing the values described above for every member of the array. In this case, the other argument may also be a string array or a cell array of character vectors (of the same size or scalar), or a character vector.

string: TF = strncmpi (A, B, n)

TF is true, if the first n characters of strings A and B are lexicographically equal, disregarding case of alphabetic characters. If one input is a string array, the other input can be a string array, a character vector, or a cell array of character vectors.

If either A or B is a string array or a cell array of character vectors, then a logical array TF of the same size is returned, containing the values described above for every member of the array. In this case, the other argument may also be a string array or a cell array of character vectors (of the same size or scalar), or a character vector.

string: newstr = append (str1, …, strN)

newstr = append (str1, …, strN) combines the text from each input argument, str1, …, strN), which must be either string arrays, cell arrays of character vectors, or character vectors or matrices. All input arguments must be of compatible sizes. Character vectors are treated as a single text element and character matrices are treated as a column of elements. append preserves any trailing white spaces, unlike the strcat function.

string: str = compose (formatSpec, A)
string: str = compose (formatSpec, A1, …, AN)
string: str = compose (txt)

str = compose (formatSpec, A) formats the data in the array A according to the formatting operators in formatSpec, which must be a string scalar or character vector, and returns the result in the string array str. The formatting operators are the same as those accepted by the sprintf function. Unlike sprintf, which returns a single character vector, compose returns a string array whose elements correspond to the rows of A.

compose applies formatSpec to each row of A so that str has the same number of rows as A. The size of str is further determined as follows:

  • If the number of columns in A exceeds the number of formatting operators in formatSpec, then formatSpec is applied repeatedly along each row of A, adding columns to str.
  • If the number of columns in A is less than the number of formatting operators, then the operators left without a corresponding value appear unchanged in str.
  • If A has zero columns, then str has the same size as A and no formatting operators are applied.

str = compose (formatSpec, A1, …, AN) formats the data from the arrays A1, …, AN. The formatting operators are assigned to the input arrays in order: once an operator has consumed a value from an input array, it becomes unavailable to the following arrays. All input arrays must be of compatible sizes.

str = compose (txt) translates escape-character sequences, such as '\n' and '\t', in txt and returns the result in str, which has the same size as txt. Any formatting operators in txt are left unchanged.

In all syntaxes, escape-character sequences appearing in literal text are translated and each '%%' literal is converted to a single '%' character, following the same rules as the sprintf function. The only difference from sprintf is that a formatting operator left without a corresponding value is emitted unchanged rather than dropped.

string: newstr = erase (str, match)

newstr = erase (str, match) removes the occurrences of match from each element of the string array str. match can be a string array, a character vector, or a cell array of character vectors. When match contains more than one piece of text, every occurrence of every element of match is removed. newstr is a string array of the same size as str; the size of match need not match the size of str. Missing values in str are preserved.

string: newstr = eraseBetween (str, startPat, endPat)
string: newstr = eraseBetween (str, startPos, endPos)
string: newstr = eraseBetween (…, "Boundaries", bounds)

newstr = eraseBetween (str, startPat, endPat) removes from each element of the string array str the text that occurs between the substrings startPat and endPat, keeping the boundary substrings themselves. startPat and endPat can be string arrays, character vectors, or cell arrays of character vectors. For each element, the first occurrence of startPat is matched and then the first occurrence of endPat that begins after it; if either boundary is not found, the element is returned unchanged.

newstr = eraseBetween (str, startPos, endPos) removes the text between the character positions startPos and endPos, inclusive of the characters at those positions. startPos and endPos must be positive integers with startPos not exceeding endPos and both within the length of the corresponding element of str.

newstr = eraseBetween (…, "Boundaries", bounds) specifies whether the boundaries are included in or excluded from the erased text. bounds can be either "inclusive" or "exclusive". When boundaries are given as substrings, the default is "exclusive" and the boundary substrings are preserved; when given as positions, the default is "inclusive" and the characters at those positions are erased.

startPat/endPat and startPos/endPos must either be scalars, applied to every element of str, or be of the same size as str and applied element-wise. newstr is a string array of the same size as str. Missing values in str are preserved.

string: newstr = extract (str, pat)
string: newstr = extract (str, pos)

newstr = extract (str, pat) returns the substrings of str that match pat. pat can be a string array, a character vector, or a cell array of character vectors; when it contains more than one piece of text, any of them may match. Within each element of str the matches are found left to right and do not overlap; where alternatives match at the same position, the first listed in pat is taken.

The matches of an element occupy a row of newstr, so the matches run along the second dimension and every element of str must yield the same number of matches. For a string scalar with n matches, newstr is 1xn; for a non-scalar str, newstr has one row per element (taken in column-major order) and one column per match. Elements with no match, including missing values, are treated as having zero matches.

newstr = extract (str, pos) returns the single character located at position pos in each element of str. pos must be a positive integer that is either a scalar, applied to every element, or the same size as str, applied element-wise. In this syntax newstr has the same size as str and missing values are preserved.

string: newstr = extractAfter (str, pat)
string: newstr = extractAfter (str, pos)

newstr = extractAfter (str, pat) returns, for each element of str, the part of the text that follows the first occurrence of pat, excluding pat itself. pat can be a string array, a character vector, or a cell array of character vectors, and must either be a scalar, applied to every element of str, or be of the same size as str and applied element-wise. If pat is not found in an element, the corresponding element of newstr is a missing value.

newstr = extractAfter (str, pos) returns the part of each element of str that follows the character position pos, that is, from pos+1 to the end. pos must be a positive integer that is either a scalar or the same size as str.

newstr is a string array of the same size as str. Missing values in str are preserved.

string: newstr = extractBefore (str, pat)
string: newstr = extractBefore (str, pos)

newstr = extractBefore (str, pat) returns, for each element of str, the part of the text that precedes the first occurrence of pat, excluding pat itself. pat can be a string array, a character vector, or a cell array of character vectors, and must either be a scalar, applied to every element of str, or be of the same size as str and applied element-wise. If pat is not found in an element, the corresponding element of newstr is a missing value.

newstr = extractBefore (str, pos) returns the part of each element of str that precedes the character position pos, that is, from the start up to pos-1. pos must be a positive integer that is either a scalar or the same size as str.

newstr is a string array of the same size as str. Missing values in str are preserved.

string: newstr = extractBetween (str, startPat, endPat)
string: newstr = extractBetween (str, startPos, endPos)
string: newstr = extractBetween (…, "Boundaries", bounds)

newstr = extractBetween (str, startPat, endPat) returns the text that occurs between the substrings startPat and endPat in each element of str. startPat and endPat can be string arrays, character vectors, or cell arrays of character vectors. Within each element the boundary pairs are matched from left to right and do not overlap, so an element may yield several substrings; these run along the second dimension, and every element of str must yield the same number of matches. For a string scalar with n matches, newstr is 1xn; for a non-scalar str, newstr has one row per element (taken in column-major order) and one column per match. Elements with no match, including missing values, are treated as having zero matches.

newstr = extractBetween (str, startPos, endPos) returns the substring between the character positions startPos and endPos, inclusive of the characters at those positions. This syntax extracts a single substring per element, so newstr has the same size as str.

newstr = extractBetween (…, "Boundaries", bounds) specifies whether the boundaries are included in or excluded from the extracted text. bounds can be either "inclusive" or "exclusive". When boundaries are given as substrings, the default is "exclusive" and the boundary substrings are not included; when given as positions, the default is "inclusive" and the characters at those positions are included.

startPat/endPat and startPos/endPos must either be scalars, applied to every element of str, or be of the same size as str. Missing values in str are preserved.

string: newstr = insertAfter (str, pat, newtext)
string: newstr = insertAfter (str, pos, newtext)

newstr = insertAfter (str, pat, newtext) inserts the text newtext into each element of str after every non-overlapping occurrence of the substring pat. pat can be a string array, a character vector, or a cell array of character vectors, and must either be a scalar, applied to every element of str, or be of the same size as str and applied element-wise. If pat is not found in an element, that element is returned unchanged.

newstr = insertAfter (str, pos, newtext) inserts newtext after the character position pos, that is, between the characters at positions pos and pos+1. pos must be a positive integer not exceeding the length of the corresponding element of str, and must be either a scalar or the same size as str.

newtext can be a string array, a character vector, or a cell array of character vectors, and must be either a scalar, inserted at every position, or of the same size as str. newstr is a string array of the same size as str. Missing values in str are preserved, and a missing value in newtext makes the corresponding element of newstr missing.

string: newstr = insertBefore (str, pat, newtext)
string: newstr = insertBefore (str, pos, newtext)

newstr = insertBefore (str, pat, newtext) inserts the text newtext into each element of str before every non-overlapping occurrence of the substring pat. pat can be a string array, a character vector, or a cell array of character vectors, and must either be a scalar, applied to every element of str, or be of the same size as str and applied element-wise. If pat is not found in an element, that element is returned unchanged.

newstr = insertBefore (str, pos, newtext) inserts newtext before the character position pos, that is, between the characters at positions pos-1 and pos. pos must be a positive integer not exceeding the length of the corresponding element of str, and must be either a scalar or the same size as str.

newtext can be a string array, a character vector, or a cell array of character vectors, and must be either a scalar, inserted at every position, or of the same size as str. newstr is a string array of the same size as str. Missing values in str are preserved, and a missing value in newtext makes the corresponding element of newstr missing.

string: newstr = replace (str, old, new)

newstr = replace (str, old, new) replaces every occurrence of the substring old with new in each element of the string array str. old and new can be string arrays, character vectors, or cell arrays of character vectors.

When old contains several substrings, new must either be a single substring, used to replace all of them, or be of the same size as old, replacing each substring of old with the corresponding element of new. All substrings are replaced in a single left to right pass over each element: at each position the substrings of old are tried in order, the first that matches is replaced, and the scan resumes past the inserted text, so replacements are not themselves re-scanned. old and new are applied to every element of str; their size need not match the size of str.

newstr is a string array of the same size as str. A substring of old that does not occur leaves the text unchanged, and missing values in str are preserved.

string: newstr = replaceBetween (str, startPat, endPat, newtext)
string: newstr = replaceBetween (str, startPos, endPos, newtext)
string: newstr = replaceBetween (…, "Boundaries", bounds)

newstr = replaceBetween (str, startPat, endPat, newtext) replaces, in each element of the string array str, the text that occurs between the substrings startPat and endPat with newtext, keeping the boundary substrings themselves. startPat and endPat can be string arrays, character vectors, or cell arrays of character vectors. For each element, the first occurrence of startPat is matched and then the first occurrence of endPat that begins after it; if either boundary is not found, the element is returned unchanged.

newstr = replaceBetween (str, startPos, endPos, newtext) replaces the text between the character positions startPos and endPos, inclusive of the characters at those positions, with newtext. startPos and endPos must be positive integers with startPos not exceeding endPos and both within the length of the corresponding element of str.

newstr = replaceBetween (…, "Boundaries", bounds) specifies whether the boundaries are included in or excluded from the replaced text. bounds can be either "inclusive" or "exclusive". When boundaries are given as substrings, the default is "exclusive" and the boundary substrings are preserved; when given as positions, the default is "inclusive" and the characters at those positions are replaced.

startPat/endPat and startPos/endPos must either be scalars, applied to every element of str, or be of the same size as str and applied element-wise. newtext can be a string array, a character vector, or a cell array of character vectors, and must be either a scalar or the same size as str. newstr is a string array of the same size as str. Missing values in str are preserved, and a missing value in newtext makes the corresponding element of newstr missing.

string: newstr = reverse (str)

newstr = reverse (str) reverses the order of the characters in every each element of the string array str. newstr is a string array of the same size as str.

string: B = sort (A)
string: B = sort (A, dim)
string: B = sort (A, direction)
string: B = sort (A, dim, direction)
string: B = sort (…, 'MissingPlacement', MP)
string: [B, index] = sort (A, …)

B = sort (A) sorts the elements of the string array A in ascending order. Elements are compared lexicographically by their Unicode code points, with the empty string "" sorting before any non-empty string. If A is a matrix, sort (A) sorts each column of A in ascending order. For multidimensional arrays, sort (A) sorts along the first non-singleton dimension.

B = sort (A, dim) sorts along the dimension specified by dim.

B = sort (A, direction) also specifies the sorting direction, which can be either 'ascend' (default) or 'descend'.

B = sort (…, 'MissingPlacement', MP) specifies where to place the missing elements (<missing>) returned in B with any of the following options specified in MP:

  • 'auto', which is the default, places missing elements last for ascending sort and first for descending sort.
  • 'first' places missing elements first.
  • 'last' places missing elements last.

[B, index] = sort (A, …) also returns a sorting index containing the original indices of the elements in the sorted array.

  • If A is a vector, then index contains the original linear indices of the elements in the sorted vector B such that B = A(index).
  • If A is an M×N matrix and dim = 1, then index contains the original row indices of the elements in the sorted vector B such that for j = 1:N, B(:,j) = A(index(:,j),j).
string: B = sortrows (A)
string: B = sortrows (A, col)
string: B = sortrows (A, direction)
string: B = sortrows (A, col, direction)
string: B = sortrows (…, 'MissingPlacement', MP)
string: [B, index] = sortrows (A, …)

B = sortrows (A) sorts the rows of the 2-D string array A in ascending order. The sorted array B has the same size as A.

B = sortrows (A, col) sorts A according to the columns specified by the numeric vector col, which must explicitly contain non-zero integers whose absolute values index existing columns in A. Positive elements sort the corresponding columns in ascending order, while negative elements sort the corresponding columns in descending order.

B = sortrows (A, direction) also specifies the sorting direction, which can be either 'ascend' (default) or 'descend' applying to all columns in A. Alternatively, direction can be either a string array or a cell array of character vectors specifying the sorting direction for each individual column of A, in which case the number of elements in direction must equal the number of columns in A.

B = sortrows (A, col, direction) sorts the string array A according to the columns specified in col using the corresponding sorting direction specified in direction. In this case, the sign of the values in col is ignored. col and direction must have the same number of elements, but not necessarily equal to the columns of A.

B = sortrows (…, 'MissingPlacement', MP) specifies where to place the missing elements (<missing>) returned in B with any of the following options specified in MP:

  • 'auto', which is the default, places missing elements last for ascending sort and first for descending sort.
  • 'first' places missing elements first.
  • 'last' places missing elements last.

[B, index] = sortrows (A, …) also returns an index vector containing the original row indices of A in the sorted matrix B such that B = A(index,:).

string: newstr = split (str)
string: newstr = split (str, delimiter)
string: newstr = split (str, delimiter, dim)
string: [newstr, match] = split (…)

newstr = split (str) divides each element of str at whitespace characters and returns the pieces as a string array. The whitespace characters are the space, tab, newline, carriage return, form feed, and vertical tab.

newstr = split (str, delimiter) divides each element at the substrings specified by delimiter, which can be a string array, a character vector, or a cell array of character vectors. When delimiter contains several substrings they are all used; at a given position the substrings are tried in order and the first that matches is taken. Delimiters are not collapsed, so consecutive delimiters yield empty strings in newstr.

The pieces of each element are laid out along a new dimension. For a string scalar that splits into n pieces, newstr is nx1; for an Mx1 column it is Mxn; for a 1xM row it is 1xMxn; and in general the pieces extend the first trailing singleton dimension. Every element of str must split into the same number of pieces. Missing values in str are preserved and count as a single piece.

newstr = split (str, delimiter, dim) lays the pieces out along dimension dim, which must be a dimension along which str has size 1.

[newstr, match] = split (…) also returns the delimiters matched between the pieces. match has the same layout as newstr but with one fewer element along the split dimension.

string: newstr = splitlines (str)

newstr = splitlines (str) divides each element of str at its newline characters and returns the lines as a string array. The recognized newline characters are the line feed, the carriage return, a carriage return followed by a line feed (treated as a single boundary), the vertical tab, the form feed, and the Unicode next-line (U+0085), line-separator (U+2028), and paragraph-separator (U+2029) characters.

The lines are laid out along a new dimension exactly as for split: a string scalar with n lines yields an nx1 array, and an Mx1 column yields an Mxn array. Every element of str must contain the same number of newlines. Missing values in str are preserved and count as a single line.

string: newstr = strcat (str1, str2, …)

newstr = strcat (str1, str2, …) merges horizontally all the input arguments into a string array, as long as any of the input arguments is a string array. All inputs must be of common size or scalars. All inputs must be character vectors, cell arrays of character vectors, or string arrays.

string: newstr = strip (str)
string: newstr = strip (str, side)
string: newstr = strip (str, stripchar)
string: newstr = strip (str, side, stripchar)

newstr = strip (str) removes all consecutive whitespace characters from the beginning and end of each element of str. The whitespace characters are the space, tab, newline, carriage return, form feed, and vertical tab.

newstr = strip (str, side) removes whitespace from the side given by side, which can be "left", "right", or "both" (the default).

newstr = strip (str, stripchar) removes the single character stripchar instead of whitespace. Because the side keywords are longer than one character, a single-character second argument is always treated as stripchar.

newstr = strip (str, side, stripchar) removes stripchar from the given side.

newstr is a string array of the same size as str. Missing values in str are preserved.

string: newstr = pad (str)
string: newstr = pad (str, nchars)
string: newstr = pad (str, side)
string: newstr = pad (str, nchars, side)
string: newstr = pad (…, padchar)

newstr = pad (str) pads each element of str with trailing spaces so that every element is as long as the longest one.

newstr = pad (str, nchars) pads each element to nchars characters. Elements that already have more than nchars characters are left unchanged; pad never truncates.

newstr = pad (str, side) and newstr = pad (str, nchars, side) add the padding on the side given by side, which can be "left", "right", or "both". The default is "right"; for "both", an odd number of pad characters places the extra one on the right.

newstr = pad (…, padchar) pads with the single character padchar instead of a space. As padchar is the last argument and a single character, it is told apart from side by its length.

Length is measured in characters, not bytes. newstr is a string array of the same size as str, and missing values in str are preserved.

string: newstr = join (str)
string: newstr = join (str, delimiter)
string: newstr = join (str, dim)
string: newstr = join (str, delimiter, dim)

newstr = join (str) combines the elements of str along the last dimension whose size is not 1, placing a single space between consecutive elements. That dimension is reduced to size 1.

newstr = join (str, delimiter) uses delimiter instead of a space. delimiter can be a string array, a character vector, or a cell array of character vectors. A scalar delimiter is placed between every pair of elements. An array delimiter supplies the text placed between consecutive elements and must have one fewer element than str along the joined dimension; its other dimensions must be 1 or match str.

newstr = join (str, dim) and newstr = join (str, delimiter, dim) combine the elements along dimension dim.

newstr has the size of str with the joined dimension reduced to 1. If any element being combined, or any delimiter placed between them, is a missing value, the corresponding element of newstr is a missing value.

string: newstr = plus (str1, str2)

newstr = lower (str) is the equivalent of the syntax newstr = str1 + str2 and appends str2 to str1. Both input arguments must be string arrays of compatible size.

string: newstr = lower (str)

newstr = lower (str) converts all upper case characters in every element of the string array str to lower case. newstr is a string array of the same size as str.

string: newstr = upper (str)

newstr = upper (str) converts all lower case characters in every element of the string array str to upper case. newstr is a string array of the same size as str.

string: B = unique (A)
string: B = unique (A, 'rows')
string: [B, ixA, ixB] = unique (…)
string: … = unique (…, order)
string: … = unique (…, occurrence)

B = unique (A) returns the unique values of the string array A in the string vector B sorted lexicographically. If If A is a column vector, then B is also a column vector, otherwise unique returns a row vector.

B = unique (A, 'rows') returns the unique rows of the string matrix A in the string matrix B sorted in lexicographical order.

[B, ixA, ixB] = unique (…) also returns index vectors ixA and ixB such that B = A(ixA) and A = B(ixB), unless the 'rows' optional argument is given, in which case B = A(ixA,:) and A = B(ixB,:).

… = unique (…, order) also specifies the order of the returned unique values. order may be either 'sorted', which is the default behavior, or 'stable', in which case the unique values are returned in order of appearance.

… = unique (…, occurrence) also specifies the which index is returned in ixA, where there are repeated values or rows (if opted) in the input categorical array. occurrence may be either 'first', which is the default and returns the index of the first occurrence of each unique value, or 'last', in which case the last occurrence of each unique value is returned.

string: C = cat (dim, A, B, …)

C = cat (dim, A, B, …) concatenates string arrays A, B, … along dimension dim. All input arrays must have the same size except along the operating dimension dim. Any of the input arrays may also be character matrixes, cell arrays of character vectors, numeric arrays, or logical arrays of compatible size.

string: C = horzcat (A, B, …)

C = horzcat (A, B, …) is the equivalent of the syntax C = [A, B, …] and horizontally concatenates the string arrays A, B, …. All input arrays must have the same size except along the second dimension. Any of the input arrays may also be character matrixes, cell arrays of character vectors, numeric arrays, or logical arrays of compatible size.

string: C = vertcat (A, B, …)

C = vertcat (A, B, …) is the equivalent of the syntax C = [A; B; …] and vertically concatenates the string arrays A, B, …. All input arrays must have the same size except along the first dimension. Any of the input arrays may also be character matrixes, cell arrays of character vectors, numeric arrays, or logical arrays of compatible size.

string: B = repmat (A, n)
string: B = repmat (A, d1, …, dN)
string: B = repmat (A, dimvec)

B = repmat (A, n) returns a string array B containing n copies of the input string array A along every dimension of A.

B = repmat (A, d1, …, dN) returns an array B containing copies of A along the dimensions specified by the list of scalar integer values d1, …, dN, which specify how many copies of A are made in each dimension.

B = repmat (A, dimvec) is equivalent to the previous syntax with dimvec = [d1, …, dN].

string: B = repelem (A, n)
string: B = repelem (A, d1, …, dN)

B = repelem (A, n) returns a string vector B containing repeated elements of the input A, which must be a string vector. If n is a scalar, each element of A is repeated n times along the non-singleton dimension of A. If n is a vector, it must have the same elements as A, in which case it specifies the number of times to repeat each corresponding element of A.

B = repelem (A, d1, …, dN) returns an array B with each element of A repeated according to the the list of input arguments d1, …, dN each corresponding to a different dimension 1:ndims (A) of the input array A. d1, …, dN must be either scalars or vectors with the same length as the corresponding dimension of A containing non-negative integer values specifying the number of repetitions of each element along the corresponding dimension.

string: B = repelems (A, R)

B = repelems (A, R) returns a string vector B containing repeated elements of the input A, which must be a string vector. R must be a 2×N matrix of integers. Entries in the first row of R correspond to the linear indexing of the elements in A to be repeated. The corresponding entries in the second row of R specify the repeat count of each element.

string: B = reshape (A, d1, …, dN)
string: B = reshape (A, …, [], …)
string: B = reshape (A, dimvec)

B = reshape (A, d1, …, dN) returns a string array B with specified dimensions d1, …, dN, whose elements are taken columnwise from the string array A. The product of d1, …, dN must equal the total number of elements in A.

B = reshape (A, …, [], …) returns a string array B with one dimension unspecified which is calculated automatically so that the product of dimensions in B matches the total elements in A, which must be divisible the product of specified dimensions. An empty matrix ([]) is used to flag the unspecified dimension.

string: B = circshift (A, n)
string: B = circshift (A, n, dim)

B = circshift (A, n) circularly shifts the elements of the string array A according to n. If n is a nonzero integer scalar, then the elements of A are shifted by n elements along the first non-singleton dimension of A. If n is a vector, it must not be longer that the number of dimensions of A with each value of n corresponding to a dimension in A. The sign of the value(s) in n specify the direction in the elements of A are shifted.

B = circshift (A, n, dim) circularly shifts the elements of the string array A along the dimension specified by dim. In this case, n must be a scalar integer value.

string: B = permute (A, dims)

B = permute (A, dims) returns the generalized transpose of the string array A by rearranging its dimensions according to the permutation vector specified in dims.

dims must index all the dimensions 1:ndims (A) of the input array A, in any order, but only once. The Nth dimension of A gets remapped to the dimension in B specified by dims(N).

string: A = ipermute (B, dims)

A = ipermute (B, dims) returns the inverse of the generalized transpose performed by the permute function. The expression ipermute (permute (A, dims), dims) returns the original array A.

dims must index all the dimensions 1:ndims (B) of the input array B, in any order, but only once. The dimension of B specified in dims(N) gets remapped to the Nth dimension of A.

string: B = transpose (A)

B = transpose (A) is the equivalent of the syntax B = A.' and returns the transpose of the string matrix A.

string: B = ctranspose (A)

B = ctranspose (A) is the equivalent of the syntax B = A' and returns the transpose of the string matrix A. For string arrays, ctranspose is identical to transpose.