Matlab - verify if a string have any number

hi everyone;
I have a string and i need to verify if there are any number.
for example
str='scramble3'
and i need a answer like, True or 1(to yes) and False or 0(to no)
Thanks

 Réponse acceptée

Joseph Cheng
Joseph Cheng le 7 Août 2014
Modifié(e) : Joseph Cheng le 7 Août 2014
you can use the function regexp.
example:
~isempty(regexp('scramble3','\d'))
would return a 1.
if you leave out the isempty check then it'll return 9 which is the index position of the number.

4 commentaires

lih jo
lih jo le 7 Août 2014
works. Thank you very much.
lih jo
lih jo le 7 Août 2014
Can i ask you something else,
do you know how can i create a new string just keeping the no vowels .
Str2=scrmbl;
thanks
Joseph Cheng
Joseph Cheng le 7 Août 2014
Modifié(e) : Joseph Cheng le 7 Août 2014
Very simple. so lets say i create a string called VOWELS;
vowels = 'aeiou';
Str1 = 'scramble3';
then if i go:
vowelIndx = ismember(Str1 ,vowels);
i would get [0 0 0 1 0 0 0 1 0]
so then if i make it
Str2 = Str1(~ismember(Str1,vowels));
we'd get Str2 without the vowels selected in vowelIndx.
lih jo
lih jo le 7 Août 2014
thank you
i already used
expression='[aeiou]'; startIndex=regexp(str,expression); str(startIndex)=[]; str2=str
i appreciate it :)

Connectez-vous pour commenter.

Plus de réponses (1)

Azzi Abdelmalek
Azzi Abdelmalek le 7 Août 2014
Modifié(e) : Azzi Abdelmalek le 7 Août 2014
str='scramble3'
idx=~isempty(regexp(str,'\d','match'))
%or
idx=any(ismember(str,'0':'9'))

Catégories

En savoir plus sur Characters and Strings dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by