Character arrays and string arrays provide storage for text data in MATLAB®.
A character array is a sequence of characters, just as a
numeric array is a sequence of numbers. A typical use is to
store short pieces of text as character
vectors, such as
c = 'Hello
A string array is a container for pieces of text. String
arrays provide a set of functions for working with text as data.
Starting in R2017a, you can create strings using double quotes,
str = "Greetings friend". To convert
data to string arrays, use the
|Convert to cell array of character vectors|
|Create character array of blanks|
|Create newline character|
|Format data into multiple strings|
|Format data into string or character vector|
|Concatenate strings horizontally|
|Convert character arrays to string arrays, leaving other arrays unaltered|
|Convert string arrays to character arrays, leaving other arrays unaltered|
|Convert string arrays at any level of cell array or structure|
|Determine if input is character array|
|Determine if input is cell array of character vectors|
|Determine if input is string array|
|Determine if input is string array with one element|
|Determine if pattern is in strings|
|Count occurrences of pattern in strings|
|Determine if strings end with pattern|
|Determine if strings start with pattern|
|Find strings within other strings|
|Read formatted data from strings|
|Delete substrings within strings|
|Delete substrings between start and end points|
|Extract substrings after specified positions|
|Extract substrings before specified positions|
|Extract substrings between start and end points|
|Insert strings after specified substrings|
|Insert strings before specified substrings|
|Add leading or trailing characters to strings|
|Remove leading and trailing characters from stringS|
|Convert strings to lowercase|
|Convert strings to uppercase|
|Reverse order of characters in strings|
|Remove trailing whitespace from ends of strings|
|Remove leading and trailing whitespace from strings|
Store and manipulate text using either character arrays or string arrays.
This example shows how to analyze text data with string arrays. It shows how to store, split, and sort text, and how to compute and collect statistics for text in a string array.
Adopt string arrays as a text data type in your own code, following these guidelines.
Test string arrays for empty strings and missing values.
Compose character arrays or string arrays that include ordinary text and data formatted to your specification.
MATLAB provides several functions to search for, replace, or extract text in string arrays and character vectors.
Compare and inspect character arrays and string arrays.
Regular expressions are a versatile way to search and replace text.