1 / 18

Strings Characters and Sentences

Strings Characters and Sentences. Overview Creating Strings Slicing Strings. 1. Strings, overview. Strings are vectors of characters Str = ‘This is 21 characters’;. Spaces count as 1 characters too!. 2. Creating Strings. Defining a string by hard-coding str = ‘ Fred Flintstone ’ ;

avalon
Download Presentation

Strings Characters and Sentences

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. StringsCharacters and Sentences Overview Creating Strings Slicing Strings

  2. 1. Strings, overview. • Strings are vectors of characters Str = ‘This is 21 characters’; Spaces count as 1 characters too!

  3. 2. Creating Strings • Defining a string by hard-coding str = ‘Fred Flintstone’; • Creating with sprintf() str = sprintf(‘Curve of f(x)=%d + %dx’,b,m); How is this used?

  4. Example: sprintf() • Prompt the user for the slope and y-intercept of a line, then plot. The title of the plot must reflect the current equation of the line.

  5. Example: sprintf(), cont. • FACT: The function title() requires 1 and only 1 string argument: title( ) • This would NOT work: title(‘Curve of %d+%dx.’, b, m) • sprintf() is used to create 1 string variable. This string variable is used in the title() command. 1 2 3

  6. Example: sprintf(), cont. %ask for slope (m) and y-intercept (b) … %create x and y data points, then plot … plot(x,y) %label plot properly str = sprintf('Curve of f(x)=%d + %dx',b,m); title(str) xlabel('x') ylabel('y')

  7. 2. Creating Strings, cont. • Array-building str = ‘Barney’; str = [str, ‘ Rubble was here’];

  8. 2. Creating Strings, cont. • Array-building str = ‘Barney’; str = [str, ‘ Rubble was here’]; • Concatenating name = 'Fred'; lastName = 'Flintstone'; str1 = [name, ' was', ' ' , 'here']; str2 = ['Hello Mr.', lastName, '. How are you?'];

  9. 2. Creating Strings, cont. • strcat() is used to combine strings, stripping (getting rid of) trailing whitespaces! (trailing = end) str = strcat('Fred ','Flintstone',' was here ','! '); Expected: Fred Flintstone was here !

  10. 2. Creating Strings, cont. • strcat() is used to combine strings, stripping (getting rid of) trailing whitespaces! (trailing = end) str = strcat('Fred ','Flintstone',' was here ','! '); Expected: Fred Flintstone was here ! Got: FredFlintstone was here! • It does NOT get rid of leading spaces.

  11. 3. Slicing Strings Assume: s = ‘abc defghi jklmnopqrst uvwxyz’; • T = s(3:6) <enter> Answer?

  12. 3. Slicing Strings Assume: s = ‘abc defghi jklmnopqrst uvwxyz’; • T = s(3:6) <enter> • X = s(1:3:10) <enter> Answer? T = ‘c de’ Answer?

  13. 3. Slicing Strings Assume: s = ‘abc defghi jklmnopqrst uvwxyz’; • T = s(3:6) <enter> • X = s(1:3:10) <enter> • U = s(end:-1:22) <enter> Answer? T = ‘c de’ Answer? X = ‘a fi’ Answer?

  14. 3. Slicing Strings Assume: s = ‘abc defghi jklmnopqrst uvwxyz’; • T = s(3:6) <enter> • X = s(1:3:10) <enter> • U = s(end:-1:22) <enter> Answer? T = ‘c de’ Answer? X = ‘a fi’ Answer? U = ‘zyxwvu t’ How is this used?

  15. Example: Extract Data • From a string, “parse” the data, i.e. extract each word separately. • Humans find the spaces first, then extracts the 3 names in a split second. • Matlab can do exactly the same!

  16. Example: Extract Data % prompt user for full name str = input('What is your full name (first middle last)? ', 's'); % Find the spaces indices = strfind(str, ' '); %indices =find(str==' '); % Extract each separate names First = str(1:indices(1)-1) Middle = str((indices(1)+1):(indices(2)-1)) Last = str((indices(2)+1):end) strfind() returns a vector containing the indices (positions) of the desired substring‘ ’. Allows multiple letters as substrings.

  17. Example: Extract Data First = str(1: indices(1)-1 ) = str(1: 5-1) = str(1:4) Middle = str(indices(1)+1 : indices(2)-1) = str( 5+1 : 10-1 ) = str(6:9) Last = str(indices(2)+1 : end) = str(10+1 : end) = str(11:end)

  18. Wrapping Up • Definition of a string? • Hard-coding a string • Creating a string by array-building • Creating a string by concatenating • New built-in functions: • sprintf() • strcat() • strfind() • Slicing strings. Use the range operator.

More Related