Main Content

strlength

Lengths of strings

Description

L = strlength(str) returns the number of characters in str.

example

Examples

collapse all

Create a string using double quotes. The result is a 1-by-1 string array, or string scalar.

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

Tips

  • To find the length of the largest array dimension of str, use the length function, not strlength.

Algorithms

strlength counts the number of code units in text. 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™.

Version History

Introduced in R2016b