string, " "
String array
Description
You can represent text in MATLAB® using string arrays where each element of a string
array stores a sequence of characters. The sequences can have different lengths without
padding, such as "yes"
and "no"
. A string array
that has only one element is also called a string
scalar.
You can index into, reshape, and concatenate string arrays using standard array
operations, and you can append text to them using the +
operator. If
a string array represents numbers, then you can convert it to a numeric array using the
double
function.
Creation
You can create a string scalar by enclosing a piece of text in double quotes.
str = "Hello, world"
str = "Hello, world"
To create a string array, you can concatenate string scalars using square brackets, just as you can concatenate numbers into a numeric array.
str = ["Mercury" "Gemini" "Apollo"; "Skylab" "Skylab B" "ISS"]
str = 2x3 string "Mercury" "Gemini" "Apollo" "Skylab" "Skylab B" "ISS"
You also can convert variables of different data types into string arrays using the
string
function, described below.
Syntax
Description
Create Strings
creates string array where each element is enclosed in a pair of double
quotes.str
= ["text1" "text2" ...]
combines
two strings using the str
= "text1" + "text2"+
operator
Convert Arrays
Input Arguments
Output Arguments
Examples
Tips
For a list of functions to create and manipulate text in string arrays, see Characters and Strings.
If the input argument is an object, then it must belong to a class that implements a
string
method to represent the object as a string.Converting a
char
array to a numeric type will produce an array of the corresponding Unicode code values. Text in strings does not convert in this way. Converting a string that does not represent a single numeric value todouble
will produce aNaN
result. For more information, see Unicode and ASCII Values.Extract elements of a string array as a character vector using curly-braced indexing. For example:
str = ["one" "two" "three"]; str{2}
ans = 'two'
Individual characters of a string array element can be extracted as a character vector using both curly-brace indexing to specify an element of the string array and normal indexing to specify which characters to extract. For example:
str = ["one" "two" "three"]; str{2}(1)
ans = 't'
Extended Capabilities
Version History
Introduced in R2016b