string, " " - String array - MATLAB (original) (raw)

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 thedouble function.

Creation

You can create a string scalar by enclosing a piece of text in double quotes.

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 thestring function, described below.

Syntax

Description

Create Strings

[str](#d126e1747822) = "text" creates a string containing text enclosed in double quotes.

example

[str](#d126e1747822) = ["text1" "text2" ...] creates string array where each element is enclosed in a pair of double quotes.

example

[str](#d126e1747822) = "text1" + "text2" combines two strings using the + operator

Convert Arrays

[str](#d126e1747822) = string([A](#d126e1747284)) converts the input array to a string array. For instance, ifA is numeric vector [1 20 300],str is a string array of the same size, ["1" "20" "300"].

example

Convert Dates and Times

[str](#d126e1747822) = string([D](#mw%5Fb6e2b88b-6616-47e8-a1c7-9b52e731dd7e),[datefmt](#mw%5F6fa739af-87ce-4cd3-b00a-a4fdf5091950)), where D is a datetime orduration array, applies the specified format, such as"HH:mm:ss".

example

[str](#d126e1747822) = string([D](#mw%5Fb6e2b88b-6616-47e8-a1c7-9b52e731dd7e),[datefmt](#mw%5F6fa739af-87ce-4cd3-b00a-a4fdf5091950),[locale](#mw%5F1ee124e1-8789-4aab-82f1-5ccba4bb9045)) specifies the locale, such as "en_US".

example

Input Arguments

expand all

Input array. The data type of A determines howstring converts A to a string array.

Input Type Conversion Notes Sample Input Sample Output
char Each Row becomes a string scalarIf A is empty, '', the output is"", a string scalar with no characters 1×3 char array 'foo' 1×1 string array "foo"
2×3 char array 'foo' 'bar' 2×1 string array "foo" "bar"
Cell array Every element of a cell array must be convertible to a 1-by-1 string. {137,'foo'} ["137" "foo"]
Categorical array Output strings are the category names corresponding to each element ofA. 1x3 categorical array red green blue 1x3 string array "red" "green" "blue"
Numeric array Output format and precision equivalent to using num2str. Use compose to specify more precise formatting.IfA is empty,[], the output is a 0-by-0 empty string array.Use char, ' ' to convert to ASCII or Unicode points. [137 3.1e-3 8.5e-6] ["137" "0.0031" "8.5e-06"]
Logical array The logical function does not accept string inputs, so the conversion is one-way. logical([0 1]) ["false" "true"]

Converted missing values, such as NaN,NaT, and <undefined> categorical values, display as <missing>.

Date or duration array, specified as a datetime,duration, or calendarDuration array. The data type of D determines howstring converts D to character vectors.

Input Type Conversion Notes Sample Input Sample Output
datetime array Converts each element to a string. To specify a format and locale, see datefmt. datetime(2020,6,1) "01-Jun-2020"
duration array Converts each element to a string. To specify a format and locale, see datefmt. duration(5:6,12,21) 1×2 string array "05:12:21" "06:12:21"
calendarDuration array Converts each element to a string. To specify a format and locale, see datefmt. calmonths(15) + caldays(8) + hours(1.2345) "1y 3mo 8d 1h 14m 4.2s"

Date format and locale, specified as separate character vectors or string scalars. Input A must be of typedatetime, duration, orcalendarDuration.

If you do not specify a format, string uses the value in the Format property of D.

Example: string(A,"yyyy-MM-dd")

The supported formats depend on the data type ofA.

Locale, specified as one of these values:

To specify only the locale, use an empty array as a placeholder for the format, [].

Example: string(A,"yyyy-MM-dd","en_US")

Example: string(A,[],"en_US")

The locale affects the language used to represent certain components of dates and times, such as month names. Valid values are:

This table lists some common values for the locale.

Locale Language Country
"de_DE" German Germany
"en_GB" English United Kingdom
"en_US" English United States
"es_ES" Spanish Spain
"fr_FR" French France
"it_IT" Italian Italy
"ja_JP" Japanese Japan
"ko_KR" Korean Korea
"nl_NL" Dutch Netherlands
"zh_CN" Chinese (simplified) China

Output Arguments

expand all

Output array, returned as a string array.

MATLAB stores all characters as Unicode® characters using the UTF-16 encoding. For more information on Unicode, see Unicode.

Examples

collapse all

Create a string scalar by enclosing a piece of text in double quotes.

Create a string array by concatenating string scalars using square brackets.

str = ["Mercury" "Gemini" "Apollo"; "Skylab" "Skylab B" "ISS"]

str = 2×3 string "Mercury" "Gemini" "Apollo" "Skylab" "Skylab B" "ISS"

To find the unique words in a string, split it on space characters and call the unique function.

First, create a string scalar.

str = "A horse! A horse! My kingdom for a horse!"

str = "A horse! A horse! My kingdom for a horse!"

Remove the exclamation point.

str = "A horse A horse My kingdom for a horse"

Convert all letters in str to lowercase characters.

str = "a horse a horse my kingdom for a horse"

Split str on space characters using the split function. split discards the space characters and returns the result as a string array.

str = 9×1 string "a" "horse" "a" "horse" "my" "kingdom" "for" "a" "horse"

Find the unique words in str using the unique function.

str = 5×1 string "a" "for" "horse" "kingdom" "my"

A = 'Four score and seven years ago'

A = 'Four score and seven years ago'

str = "Four score and seven years ago"

str contains the same characters as A. But while A is a character vector, str is a string scalar.

To return the number of characters in str, use the strlength function.

Convert a cell array of character vectors to a string array.

A = {'Mercury','Gemini','Apollo';... 'Skylab','Skylab B','ISS'}

A = 2×3 cell {'Mercury'} {'Gemini' } {'Apollo'} {'Skylab' } {'Skylab B'} {'ISS' }

str = 2×3 string "Mercury" "Gemini" "Apollo" "Skylab" "Skylab B" "ISS"

To access the second element in the first row of str, index using smooth parentheses. You can access strings in a string array with matrix indexing, just as you would access elements of a numeric array.

Access the third column.

ans = 2×1 string "Apollo" "ISS"

A = 1×6

77    65    84    76    65    66

str = 1×6 string "77" "65" "84" "76" "65" "66"

str is a string array in which each element represents a number from A. Note that string does not treat numbers as ASCII or Unicode® values the way that the char function does.

Create a string array in which each element represents a number. To convert the string array to a numeric array, use the double function.

str = ["256","3.1416","8.9e-3"]

str = 1×3 string "256" "3.1416" "8.9e-3"

X = 1×3

256.0000 3.1416 0.0089

When the input argument is a string array, the double function treats each element as the representation of a floating-point value. However, when the input is a character array, double instead converts each character to a number representing its Unicode® value.

As an alternative, use the str2double function. str2double is suitable when the input argument might be a string array, character vector, or cell array of character vectors.

Y = 1×3

256.0000 3.1416 0.0089

C = '2.7183'; Z = str2double(C)

Convert from a duration array to string. For more information related to converting from common data types to string see Convert Between Text and datetime or duration Values.

Create a duration array.

D = hours(23:25) + minutes(8) + seconds(1.2345)

D = 1×3 duration 23.134 hr 24.134 hr 25.134 hr

Convert D to a string array.

str = 1×3 string "23.134 hr" "24.134 hr" "25.134 hr"

str is a string array with one duration value per element. str is the same size as D.

Specify the format of the duration values in str.

str = 1×3 string "23:08" "24:08" "25:08"

Create a datetime.

D = datetime 01-Feb-2025 08:47:33

Convert the datetime to a string that is formatted and localized to france.

C = string(D,'eeee, MMMM d, yyyy HH:mm:ss',"fr_FR")

C = "samedi, février 1, 2025 08:47:33"

Tips

Extended Capabilities

expand all

Thestring, " " function fully supports tall arrays. For more information, see Tall Arrays.

Usage notes and limitations:

Version History

Introduced in R2016b