Matlab - verify if a string have any number

2 views (last 30 days)
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

Accepted Answer

Joseph Cheng
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
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.
lih jo
lih jo on 7 Aug 2014
thank you
i already used
expression='[aeiou]'; startIndex=regexp(str,expression); str(startIndex)=[]; str2=str
i appreciate it :)

Sign in to comment.

More Answers (1)

Azzi Abdelmalek
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'))

Categories

Find more on Characters and Strings in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!