how I can know if a string is empty or not!?
67 ビュー (過去 30 日間)
古いコメントを表示
採用された回答
Guillaume
2016 年 5 月 15 日
編集済み: Guillaume
2016 年 5 月 15 日
>>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
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
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
2016 年 5 月 15 日
編集済み: Weird Rando
2016 年 5 月 15 日
You can use the length()
a = '';
stringlen = length(a) % stringlen = 0
参考
カテゴリ
Help Center および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!