length of a string

23 次查看(过去 30 天)
William
William 2011-9-29
Is there a function that can return the length of a string? I have tried using size. but for some reason it is not going so well.
  1 个评论
Jan
Jan 2011-9-29
Please post an exact error description than "is not going well" in the future. Posting the error message is more helpful.

请先登录,再进行评论。

采纳的回答

Fangjun Jiang
Fangjun Jiang 2011-9-29
str='abcdefe'
size(str)
length(str)
numel(str)

更多回答(2 个)

Johannes Kalliauer
Johannes Kalliauer 2018-1-11
If you have string not a char then you have to use a different command to get the number of chars: strlength
str=string('abcdefe')
strlength(str)
or you can convert the string to a char and then deterimise the length with the code posted by @Fangjun Jiang
str=string('abcdefe')
chr=char(str)
size(chr,2)
length(chr)
numel(chr)
but if you are interested in the number of lines use:
strSplited = splitlines(stringWithLinebreaks);
size(strSplited,1)
length(strSplited)
numel(strSplited)
  1 个评论
Walter Roberson
Walter Roberson 2018-1-11
Note that in 2011 when the question was originally asked, the string object did not exist and "string" often referred to a character vector.
I think it would have been better if they had used a different datatype name to avoid confusion.

请先登录,再进行评论。


Daniel Shub
Daniel Shub 2011-9-29
Have you created a variable called size? what do you get with
which size
It should be something like ../matlab/r2011a/toolbox/matlab/elmat/size

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by