Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

problem in compare strings

Asked by huda nawaf

huda nawaf

on 31 Jul 2012
Accepted Answer by per isakson

per isakson

hi,

if I want compare two strings

ex.

str1='Animation|Children's|Comedy'

i want to check just the first word and whatever the others words

str1='Animation*' str2= 'Animation'

i.e can I compare strcmp(str1,str2)?

thanks

0 Comments

huda nawaf

huda nawaf

Tags

Products

No products are associated with this question.

2 Answers

Answer by per isakson

per isakson

on 31 Jul 2012
Accepted answer

Try this

    %%
    str1 = 'Animation|Children''s|Comedy';
    str2 = 'Animation*';
    str3 = 'Animation';
    ism = strcmp( str3, regexp( str1, '\|', 'split' ) );
    >> whos ism
      Name      Size            Bytes  Class      Attributes
      ism       1x3                 3  logical              
    >> ism
    ism =
         1     0     0

Note

  1. double blip in "Children''s"
  2. strcmp make exact comparisons of strings - no wildcards as in 'Animation*';
  3. strfind is an alternative to strcmp - see the documentation

1 Comment

huda nawaf

huda nawaf

on 31 Jul 2012

thanks

per isakson

per isakson

Answer by Jan Simon

Jan Simon

on 31 Jul 2012

strncmp :

str1 = 'Animation*;
str2 = 'Animation';
strncmp(str1, str2, length(str2))

1 Comment

huda nawaf

huda nawaf

on 31 Jul 2012

thanks

Jan Simon

Jan Simon

Contact us