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

조회 수: 94 (최근 30일)
Imen Mani
Imen Mani 2016년 5월 15일
편집: Stephen23 2020년 5월 8일
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에서 Cell Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by