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

 채택된 답변

Guillaume
Guillaume 2016년 5월 15일
편집: Guillaume 2016년 5월 15일

1 개 추천

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

Imen Mani
Imen Mani 2016년 5월 15일
Ok ! thank you for your help :)
Note that isempty() works for chars but not strings.
>>isempty("aaa")
ans=
0
>>isempty("")
ans=
0
So if one is not sure if the variable is a char array or string, variable should be compared to "" with strcmp or "==".
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일

2 개 추천

You can use the length()
a = '';
stringlen = length(a) % stringlen = 0

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

질문:

2016년 5월 15일

편집:

2020년 5월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by