This is machine translation

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

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.


Length of strings in string array


L = strlength(str)



L = strlength(str) returns the number of characters in each element of the input string array. The size of L is the same as the size of str.


collapse all

Starting in R2017a, you can create strings using double quotes.

str = "Hello, World"
str = 
"Hello, World"

Return the number of characters in str.

L = strlength(str)
L = 12

Create a string array using the [] operator. str is a 2-by-3 string array that contains six strings.

str = ["Amis","Chekhov","Joyce";"Stein","","Proust"]
str = 2x3 string array
    "Amis"     "Chekhov"    "Joyce" 
    "Stein"    ""           "Proust"

Find the length of each string in str. Use strlength, not length, to determine the number of characters in each element of a string array.

L = strlength(str)
L = 2×3

     4     7     5
     5     0     6

Create a character vector. To return the number of characters in the character vector, use the strlength function.

chr = 'The rain in Spain.'
chr = 
'The rain in Spain.'
L = strlength(chr)
L = 18

Input Arguments

collapse all

Input text, specified as a string array, a character vector, or a cell array of character vectors.


To find the number of characters in each element of str, use strlength. To find the length of the largest array dimension of str, use the length function.


strlength counts the number of code units in a string. Code units are bit sequences for encoding characters of a character encoding system. In some character encodings, such as UTF-16, there are some characters that are encoded with multiple code units.

If you have a string or a character vector that contains such characters, then the number of code units is greater than the number of characters.

length(C) also returns the number of code units when C is a character vector.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Introduced in R2016b