How to check the numbers of characters of a string?

e.g I have a string, abcde. And I need a checker to check whether the string has 5 characters before executing a command.

 采纳的回答

str = num2str([17;10],'%05i')
if length(str(1,:))==5,
%%%here is your command
end
or
str = num2str([17;10],'%05i')
if numel(str(1,:))==5,
%%%here is your command
end

3 个评论

This won't work, consider:
num2str([17;10],'%05i')
ans =
00017
00010
length(ans)
Better to use NUMEL, for generality.
S = ['ertyu';'poiuy';'lkjhg';'nbvcx']
length(S)
numel(S)
I should have hit refresh, I guess ;-).

请先登录,再进行评论。

更多回答(4 个)

>>a='string'; >>n=length(a) so n will return length of a string
I converted my string into vector form and cannot count using length(str).
Thank you everybody! I figured it out. I need to char(vec(pos)) first to convert to string then I can compare with the above methods.

类别

帮助中心File Exchange 中查找有关 Develop Apps Using App Designer 的更多信息

产品

标签

Community Treasure Hunt

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

Start Hunting!

Translated by