how I can know if a string is empty or not!?

59 次查看(过去 30 天)
please help me with a code ! thank you !

采纳的回答

Guillaume
Guillaume 2016-5-15
编辑:Guillaume 2016-5-15
How about the helpfully named isempty?:
>>isempty('aaa')
ans =
0
>>isempty('')
ans =
1
Note that because strings are just matrices (of characters), it's the same test you use for matrix emptiness.
  4 个评论
Adam Danz
Adam Danz 2020-5-7
编辑:Adam Danz 2020-5-7
For strings or char arrays, you can use,
TF = strlength(s)==0; % r2016b or later
Demo
strlength('word') % ans = 4
strlength("word") % ans = 4
strlength("") % ans = 0
strlength('') % ans = 0
If the string is scalar,
isempty(char(s))
Demo
s = "";
isempty(char(s)) % ans = 1
Stephen23
Stephen23 2020-5-8
编辑:Stephen23 2020-5-8
"" is not an empty string array, it is a scalar string array (which happens to have zero characters, but the number of characters is totally irrelevant to the size of the string array):
>> isscalar("aaaaaaaaa")
ans=
1
>> isscalar("")
ans=
1
If you want to know how many characters are in the elements of a string array, use strlength:

请先登录,再进行评论。

更多回答(1 个)

Weird Rando
Weird Rando 2016-5-15
编辑:Weird Rando 2016-5-15
You can use the length()
a = '';
stringlen = length(a) % stringlen = 0

类别

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