Matlab - verify if a string have any number
2 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Joseph Cheng
on 7 Aug 2014
Edited: Joseph Cheng
on 7 Aug 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 Comments
Joseph Cheng
on 7 Aug 2014
Edited: Joseph Cheng
on 7 Aug 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.
More Answers (1)
Azzi Abdelmalek
on 7 Aug 2014
Edited: Azzi Abdelmalek
on 7 Aug 2014
str='scramble3'
idx=~isempty(regexp(str,'\d','match'))
%or
idx=any(ismember(str,'0':'9'))
0 Comments
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!