34
1. Comparing Strings 2. Converting strings/numbers 3. Additional String Functions Strings Built-In Functions 1

1. Comparing Strings 2. Converting strings/numbers 3. Additional String Functions Strings Built-In Functions 1

Embed Size (px)

DESCRIPTION

1. Comparing Strings Curious: What would using == do? %hardcode a string. Set in stone. We know what it is. str = ‘Fred’; %test in command window, whether str is equal to ‘Fred’ %(note that it is, so we expect a true!) >> str == 'Fred' ans =

Citation preview

Page 2: 1. Comparing Strings 2. Converting strings/numbers 3. Additional String Functions Strings Built-In Functions 1

2

1. Comparing Strings Curious: What would using == do?

%hardcode a string. Set in stone. We know what it is.str = ‘Fred’;

%test in command window, whether str is equal to ‘Fred’%(note that it is, so we expect a true!)>> str == 'Fred' <enter>

Page 3: 1. Comparing Strings 2. Converting strings/numbers 3. Additional String Functions Strings Built-In Functions 1

3

1. Comparing Strings Curious: What would using == do?

%hardcode a string. Set in stone. We know what it is.str = ‘Fred’;

%test in command window, whether str is equal to ‘Fred’%(note that it is, so we expect a true!)>> str == 'Fred' <enter>ans = 1 1 1 1

Page 4: 1. Comparing Strings 2. Converting strings/numbers 3. Additional String Functions Strings Built-In Functions 1

4

1. Comparing Strings Curious: What would using == do?

%hardcode a string. Set in stone. We know what it is.str = ‘Fred’;

%test in command window, whether str is equal to ‘Fred’%(note that it is, so we expect a true!)>> str == 'Fred' <enter>ans = 1 1 1 1

Matlab compares and evaluates the equality-condition between each letter 1 by 1, to a 0 (for false) or a 1 (for true)

Page 5: 1. Comparing Strings 2. Converting strings/numbers 3. Additional String Functions Strings Built-In Functions 1

5

1. Comparing Strings, cont. Curious: What would using == do?

%hardcode a string. Set in stone. We know what it is.str = ‘Fred’;

%test in command window, whether str is equal to ‘Frog’%(note that it is not, so we expect a false!)>> str == 'Frog' <enter>ans = 1 1 0 0

Page 6: 1. Comparing Strings 2. Converting strings/numbers 3. Additional String Functions Strings Built-In Functions 1

6

1. Comparing Strings, cont. Curious: What would using == do?

%hardcode a string. Set in stone. We know what it is.str = ‘Fred’;

%test in command window, whether str is equal to ‘Frog’%(note that it is not, so we expect a false!)>> str == 'Frog' <enter>ans = 1 1 0 0

2 letters were identical, 2 letters were not. But.. there is no overall true or false. This is not the way to compare strings.

Page 7: 1. Comparing Strings 2. Converting strings/numbers 3. Additional String Functions Strings Built-In Functions 1

7

1. Comparing Strings, cont. Curious: What would using == do?

%hardcode a string. Set in stone. We know what it is.str = ‘Fred’;

%test in command window, is str equal to ‘Flinstones’?%(note that it is not, so we expect a false!)>> str == 'Flintstone’ <enter>??? Error using ==> eqMatrix dimensions must agree.

It even gets worse when the length of each string does not match.. It creates an error. Definitely not the right method to compare strings..

Page 9: 1. Comparing Strings 2. Converting strings/numbers 3. Additional String Functions Strings Built-In Functions 1

9

1. Comparing Strings, cont. Two built-in functions are commonly used to compare

strings:

1. strcmp() – STRing CoMPare returns true if the two arguments are identical strings

Practice: strcmp(‘hi’, ‘hi’) evaluates to ______Practice: strcmp(‘HI’, ‘hi’) evaluates to ______

Page 10: 1. Comparing Strings 2. Converting strings/numbers 3. Additional String Functions Strings Built-In Functions 1

10

1. Comparing Strings, cont. Two built-in functions are commonly used to compare

strings:

1. strcmp() – STRing CoMPare returns true if the two arguments are identical strings

Practice: strcmp(‘hi’, ‘hi’) evaluates to ______Practice: strcmp(‘HI’, ‘hi’) evaluates to ______Practice:

str = ‘yes’;strcmp(str, ‘no’) evaluates to _____

Page 11: 1. Comparing Strings 2. Converting strings/numbers 3. Additional String Functions Strings Built-In Functions 1

11

1. Comparing Strings, cont. Two built-in functions are commonly used to compare

strings:

1. strcmp() – STRing CoMPare returns true if the two arguments are identical strings

Practice: strcmp(‘hi’, ‘hi’) evaluates to ______Practice: strcmp(‘HI’, ‘hi’) evaluates to ______Practice:

str = ‘yes’;strcmp(str, ‘no’) evaluates to _____strcmp(‘no’, str) evaluates to _____

Page 12: 1. Comparing Strings 2. Converting strings/numbers 3. Additional String Functions Strings Built-In Functions 1

12

1. Comparing Strings, cont. Two built-in functions are commonly used to compare

strings:

2. strcmpi() – STRing CoMPare Insensitivereturns true if the two arguments are the same string WITHOUT

REGARD TO CASE

Practice: strcmpi(‘hi’, ‘hi’) evaluates to ______

Page 13: 1. Comparing Strings 2. Converting strings/numbers 3. Additional String Functions Strings Built-In Functions 1

13

1. Comparing Strings, cont. Two built-in functions are commonly used to compare

strings:

2. strcmpi() – STRing CoMPare Insensitivereturns true if the two arguments are the same string WITHOUT

REGARD TO CASE

Practice: strcmpi(‘hi’, ‘hi’) evaluates to ______Practice: strcmpi(‘HI’, ‘hi’) evaluates to ______

Page 14: 1. Comparing Strings 2. Converting strings/numbers 3. Additional String Functions Strings Built-In Functions 1

14

1. Comparing Strings, cont. Two built-in functions are commonly used to compare

strings:

2. strcmpi() – STRing CoMPare Insensitivereturns true if the two arguments are the same string WITHOUT

REGARD TO CASE

Practice: strcmpi(‘hi’, ‘hi’) evaluates to ______Practice: strcmpi(‘HI’, ‘hi’) evaluates to ______Practice:

str = ‘yes’;strcmpi(str, ‘Yes’) evaluates to _____

Page 15: 1. Comparing Strings 2. Converting strings/numbers 3. Additional String Functions Strings Built-In Functions 1

15

1. Comparing Strings, cont. Two built-in functions are commonly used to compare

strings:

2. strcmpi() – STRing CoMPare Insensitivereturns true if the two arguments are the same string WITHOUT

REGARD TO CASE

Practice: strcmpi(‘hi’, ‘hi’) evaluates to ______Practice: strcmpi(‘HI’, ‘hi’) evaluates to ______Practice:

str = ‘yes’;strcmpi(str, ‘Yes’) evaluates to _____strcmpi(‘YeS’, str) evaluates to _____

Page 17: 1. Comparing Strings 2. Converting strings/numbers 3. Additional String Functions Strings Built-In Functions 1

Example: Access Granted% ask for usernameusername = input(‘Enter username: ‘, ‘s’);

if strcmpi(username, ‘John’) % correct username%ask passwd pass = input(‘Enter password: ’, ‘s’);if strcmp(pass, ‘u23!9s2’) %if correct password

%grant access...else

%quit/end code...end

else % quit/end code...end

17

The user name may not be case-sensitive…

A password is case-sensitive.

Page 20: 1. Comparing Strings 2. Converting strings/numbers 3. Additional String Functions Strings Built-In Functions 1

20

Example: prompting inputs Currently can be done as a combination of an fprintf() and an input() command:

%loop to prompt for each valuefor position = 1:nbSensors fprintf('Enter value of sensor #%d:',position); table(position) = input(' '); %leave a spaceend

Page 21: 1. Comparing Strings 2. Converting strings/numbers 3. Additional String Functions Strings Built-In Functions 1

21

Example: prompting inputs Currently can be done as a combination of an fprintf() and an input() command:

%loop to prompt for each valuefor position = 1:nbSensors fprintf('Enter value of sensor #%d:',position); table(position) = input(' '); %leave a spaceend

CAUTION: they cannot be combined. The input() command accepts only 1 string argument, or 2 when prompting for a string (‘s’).

input() never accepts placeholders and variable names.

Page 22: 1. Comparing Strings 2. Converting strings/numbers 3. Additional String Functions Strings Built-In Functions 1

22

Example: prompting inputs Can be done using string manipulations as well.

%loop to prompt for each valuefor position = 1:nbSensors

table(position) = input(['Enter value of sensor #', int2str(position) , ': ']);

end

CAUTION: the [] must be there to concatenate the 3 pieces (first part of the sentence, the number, and the colon) into 1 string.

Page 23: 1. Comparing Strings 2. Converting strings/numbers 3. Additional String Functions Strings Built-In Functions 1

23

Example: prompting inputs Can be done using string manipulations as well.

%loop to prompt for each valuefor position = 1:nbSensors

table(position) = input(['Enter value of sensor #', int2str(position) , ': ']);

end

CAUTION: the [] must be there to concatenate the 3 pieces (first part of the sentence, the number, and the colon) into 1 string.

Convert the integer to a string before concatenating all 3 pieces. The digit 1 becomes the string ‘1’, the digit 2 becomes the string ‘2’, etc.. Why:_________

Page 24: 1. Comparing Strings 2. Converting strings/numbers 3. Additional String Functions Strings Built-In Functions 1

Example: validate input Some students have wondered how to handle this issue:

24

“What happens when the user enters letters (instead of numbers)?”

Note: as long as variables d and we do not exist, Matlab loops for us. But “what if”…

Page 25: 1. Comparing Strings 2. Converting strings/numbers 3. Additional String Functions Strings Built-In Functions 1

Example: validate input, cont. To solve this problem, every input must now be

considered as a string, even if they are numbers!

Algorithms possible% Grab user’s input as strings% Use str2double() to convert to numbers% Use isnan() to check if the conversion worked% Continue with calculations if it did

25

Page 26: 1. Comparing Strings 2. Converting strings/numbers 3. Additional String Functions Strings Built-In Functions 1

Example: validate input, cont. To solve this problem, every input must now be

considered as a string, even if they are numbers!

Algorithms possible% Grab user’s input as strings% Use str2double() to convert to numbers% Use isnan() to check if the conversion worked% Continue with calculations if it did

% Grab user’s input as strings% Use str2double() to convert to numbers% while isnan() is true

% grab again, convert again% Continue with calculations if it did 26

Page 32: 1. Comparing Strings 2. Converting strings/numbers 3. Additional String Functions Strings Built-In Functions 1

Wrapping Up There are many built-in functions for strings

Using == to compare strings is really tricky Same length Logical vector returned, not just a 0 or a 1

Use strcmp() to compare 2 strings, case-sensitive Use strcmpi() to compare 2 strings, case-Insensitive

32

Page 33: 1. Comparing Strings 2. Converting strings/numbers 3. Additional String Functions Strings Built-In Functions 1

Wrapping Up There are many built-in functions for strings

Using == to compare strings is really tricky Same length Logical vector returned, not just a 0 or a 1

Use strcmp() to compare 2 strings, case-sensitive Use strcmpi() to compare 2 strings, case-Insensitive There is no mean to compare 3 or 4 strings together.

33

Page 34: 1. Comparing Strings 2. Converting strings/numbers 3. Additional String Functions Strings Built-In Functions 1

Wrapping Up There are many built-in functions for strings

Using == to compare strings is really tricky Same length Logical vector returned, not just a 0 or a 1

Use strcmp() to compare 2 strings, case-sensitive Use strcmpi() to compare 2 strings, case-Insensitive There is no mean to compare 3 or 4 strings together.

str2num(), str2double() converts string numbers num2str(), int2str() converts numbers strings

34