Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

string

Convert to string array

Syntax

str = string(A)
str = string(D)
str = string(D,fmt)
str = string(D,fmt,locale)

Description

example

str = string(A) converts the input array to a string array. For more information on string arrays, see Characters and Strings.

  • If A is a character vector, then string converts A to a string scalar. str and A have the same characters in the same order.

  • If A is a cell array, then string converts each element in A into a string element in str.

  • If A is a numeric array, then string converts each number to a string element in str.

    Unlike the char function, string does not treat numbers as ASCII or Unicode® code points.

  • If A is a logical array, then string converts each value to either "false" or "true".

  • If A is [], then string returns a 0-by-0 string array.

Note: Starting in R2017a, you can create strings using double quotes instead of the string function.

example

str = string(D) converts a datetime, duration, or calendar duration array into a string array in the format specified by the Format property of D. The output contains one date or duration in each row.

example

str = string(D,fmt) represents dates or durations in the specified format, such as 'HH:mm:ss'.

str = string(D,fmt,locale) represents dates or durations in the specified locale, such as 'en_US'. The locale affects the language used to represent character vectors such as month and day names.

Examples

collapse all

A = 'Four score and seven years ago'
A = 
'Four score and seven years ago'
str = string(A)
str = 
"Four score and seven years ago"

str is a string scalar containing the same characters as A.

Starting in R2017a, you also can create a string using double quotes.

str = "To be, or not to be"
str = 
"To be, or not to be"

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

A = {'Mercury','Gemini','Apollo';...
     'Skylab','Skylab B','ISS'}
A = 2×3 cell array
    'Mercury'    'Gemini'      'Apollo'
    'Skylab'     'Skylab B'    'ISS'   

str = string(A)
str = 2×3 string array
    "Mercury"    "Gemini"      "Apollo"
    "Skylab"     "Skylab B"    "ISS"   

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

str(1,2)
ans = 
"Gemini"

Access the third column.

str(:,3)
ans = 2×1 string array
    "Apollo"
    "ISS"

Starting in R2017a, you also can create a string array using double quotes and the [] operator.

str = ["Mercury","Venus","Earth"]
str = 1×3 string array
    "Mercury"    "Venus"    "Earth"

A = [77 65 84 76 65 66]
A = 

    77    65    84    76    65    66

str = string(A)
str = 1×6 string array
    "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.

Create a duration array.

D = hours(23:25) + minutes(8) + seconds(1.2345)
D = 1×3 duration array
   23.134 hr   24.134 hr   25.134 hr

Convert D to a string array.

str = string(D)
str = 1×3 string array
    "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 = string(D,'hh:mm')
str = 1×3 string array
    "23:08"    "24:08"    "25:08"

Input Arguments

collapse all

Input array.

Data Types: char | cell | string | double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | categorical

Input date and time, specified as a date or duration array.

Data Types: datetime | duration | calendarDuration

Date format, specified as a character vector. The supported formats depend on the data type of input D.

  • datetime formats can include combinations of units and delimiters, such as 'yyyy-MMM-dd HH:mm:ss.SSS'. For details, see the Format property for datetime arrays.

  • duration formats are either single characters ('y', 'd', 'h', 'm', or 's') or one of these combinations:

    • 'dd:hh:mm:ss'

    • 'hh:mm:ss'

    • 'mm:ss'

    • 'hh:mm'

    • Any of the above, with up to nine S characters to indicate fractional second digits, such as 'hh:mm:ss.SSSS'

  • calendarDuration formats can include combinations of the characters 'y', 'q', 'm', 'w', 'd', and 't' in order from largest to smallest unit of time, such as 'ym'.

For more information on the duration and calendarDuration formats, see Set Date and Time Display Format.

Locale of the character vectors to create, specified as a character vector.

locale can be:

  • 'system', to specify your system locale.

  • a character vector in the form xx_YY, where xx is a lowercase ISO 639-1 two-letter code that specifies a language, and YY is an uppercase ISO 3166-1 alpha-2 code that specifies a country.

These are the same character vectors accepted by the 'Locale' name-value pair argument for the datetime function. The locale affects the language used in the output array.

Example: 'en_US'

Example: 'ja_JP'

Output Arguments

collapse all

Output array, returned as a string array.

Text processing functions (such as strfind and regexp) accept string arrays as inputs, but other functions (for example, addpath) do not.

Tips

  • 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.

  • To convert scalar strings to character vectors, use the char function.

  • To convert string arrays to cell arrays of character vectors, use the cellstr function.

  • To convert string arrays to numeric arrays, use the double function.

Extended Capabilities

Introduced in R2016b

Was this topic helpful?