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.

strlength

Length of strings in string array

Syntax

L = strlength(str)

Description

example

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.

Examples

collapse all

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

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 = 

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

Algorithms

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

Was this topic helpful?