1 / 18

Strings Built-In Functions

Strings Built-In Functions. Comparing Strings Converting strings/numbers Additional String Functions. 1. Comparing Strings. Curious: What would using == do between strings? >> 'hi' == 'by' <enter> ans = ___________________ >> 'HI' == 'hi' < enter> ans = ___________________

alta
Télécharger la présentation

Strings Built-In Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. StringsBuilt-In Functions Comparing Strings Converting strings/numbers Additional String Functions

  2. 1. Comparing Strings • Curious: What would using == do between strings? >> 'hi' == 'by' <enter> ans = ___________________ >> 'HI' == 'hi' <enter> ans= ___________________ >> 'yes' == 'no' <enter> ans= ___________________ >> 'word1'== 'answer1'<enter> ans = ___________________

  3. 1. Comparing Strings • Curious: What would using == do? • MATLAB evaluates equality letter by letter, assigning a 0 (for false) or a 1 (for true) • THIS ONLY WORKS FOR STRINGS OF IDENTICAL LENGTHS!!! • so it can't possibly be the way to go in 99% of the cases…

  4. 1. Comparing Strings, cont. • Two built-in functions are commonly used to compare strings: • strcmp()– STRingCoMPare returns true if the two arguments (both strings) are identical (CaSesEnSItiVE) Practice: strcmp('hi', 'hi') evaluates to ______ Practice: strcmp('HI', 'hi') evaluates to ______ Practice: str = 'yes'; strcmp(str, ‘yes') evaluates to _____ strcmp(‘yes',str) evaluates to _____

  5. 1. Comparing Strings, cont. • Two built-in functions are commonly used to compare strings: • strcmpi()– STRingCoMPare Insensitive returns true if the two arguments (both strings) are identical 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 _____

  6. Example: Access Granted % ask for username username = input('Enter username: ', 's'); if %correct username % ask for a passwd if %correct password grant access… else quit/end code end else % quit/end code end if time..

  7. Example: Access Granted % ask for username username = input('Enter username: ', 's'); ifstrcmpi(username, 'John') % correct username %ask passwd pass = input('Enter password: ', 's'); ifstrcmp(pass, 'u23!9s2')%if correct password %grant access... else %quit/end code... end else % quit/end code... end The user name may not be case-sensitive… …but a password is usually case-sensitive this should probably be changed to a loop…

  8. Again… BAD!!! GOOD!!! fprintf('Blindfolded. Type two words (hit enter in between each)!\n'); %gettwowords w1 = input('Enter word1: '); w2 = input('Enter word2: '); ifstrcmp(w1,w2) == 1 %equalwords fprintf('Yay!!! Identical words!\n'); else fprintf('Booo.... unidentical\n'); end fprintf('Blindfolded. Type two words (hit enter in between each)!\n'); %gettwowords w1 = input('Enter word1: '); w2 = input('Enter word2: '); if w1 == w2 %equalwords fprintf('Yay!!! Identical words!\n'); else fprintf('Booo.... unidentical\n'); end the == 1 is not necessary. if 1 in itself means if true!

  9. 2. Converting strings • Convert: string  numbers str2num() str2double() [will convert an entire cell arrays of strings] • Convert: number  string int2str() num2str()

  10. Example: Validating input • “What happens when the user enters letters (instead of numbers)?”

  11. Example: Validating input • To solve this problem, every input must now be considered as a string, even if they are numbers! • Algorithm % Grab user's input as a string % Use str2double() to convert to numbers % while isnan() is true % grab again, convert again % Continue with calculations if it did if time..

  12. Example: Validating input • What does str2double() and isnan() do? • Example when string does look like a number:

  13. Example: Validating input • What does str2double() and isnan() do? • Example when string has an ERROR:

  14. Example: Validating input • Now, prompt the user for a value, then put the str2double()and isnan()in a loop! EXTREMELY IMPORTANT: isnan()MUST BE THE FIRST CONDITION.

  15. Get Bounded Integer • Write a function that accepts a prompt, a lower limit, and an upper limit and validate the input, re-prompting until the user enters a valid number. • Algorithm: % Print prompt and collect input as a string % Identify if input is a number % Identify if input is between lower and upper limits % As long as input is invalid, print an error message and re-prompt

  16. Main code clc clear %prompt age age = getBoundedInt('Enter your age (1-100): ',1,100); %prompt how many kids nbKids = getBoundedInt('\nHow many kids do you have (1-30): ',1,30); %prompt how many house you own nbHouses = getBoundedInt('\nHow many houses do you own (1-5): ',1,5); … huh? no loop???

  17. ReUsable Function!! functionValidIntNum = getBoundedInt( Prompt, LowerLimit, UpperLimit ) % GETBOUNDEDINT % ValidIntNum= getBoundedInt( Prompt, LowerLimit, UpperLimit); % Prompts for an integer between 2 values. Returns a valid integer. %Print prompt and collect input as a string ValidIntStr= input(Prompt, 's'); ValidIntNum = str2double(ValidIntStr); %convert string to double %Identify if input is a number, is between lower and upper limits, is whole whileisnan(ValidIntNum) || ValidIntNum<LowerLimit|| ValidIntNum>UpperLimit || … floor(ValidIntNum) ~= ValidIntNum %print an error message and re-prompt fprintf('Enter a valid integer between %d and %d\n', LowerLimit, UpperLimit ); ValidIntStr= input(Prompt, 's'); ValidIntNum= str2double(ValidIntStr); %convert string to double end

  18. 3. Additional String Functions • lower() – converts a string to lowercase • upper() – converts a string to uppercase • isletter() – which characters in the string are letters?

More Related