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

 Respuesta aceptada

Guillaume
Guillaume el 15 de Mayo de 2016
Editada: Guillaume el 15 de Mayo de 2016
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 comentarios

Imen Mani
Imen Mani el 15 de Mayo de 2016
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 el 7 de Mayo de 2020
Editada: Adam Danz el 7 de Mayo de 2020
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 el 8 de Mayo de 2020
Editada: Stephen23 el 8 de Mayo de 2020
"" 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:

Iniciar sesión para comentar.

Más respuestas (1)

Weird Rando
Weird Rando el 15 de Mayo de 2016
Editada: Weird Rando el 15 de Mayo de 2016
You can use the length()
a = '';
stringlen = length(a) % stringlen = 0

Categorías

Más información sobre Characters and Strings en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 15 de Mayo de 2016

Editada:

el 8 de Mayo de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by