190 likes | 371 Views
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 ’ ;
E N D
StringsCharacters 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’; • Creating with sprintf() str = sprintf(‘Curve of f(x)=%d + %dx’,b,m); How is this used?
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.
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
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')
2. Creating Strings, cont. • Array-building str = ‘Barney’; str = [str, ‘ Rubble was here’];
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?'];
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 !
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.
3. Slicing Strings Assume: s = ‘abc defghi jklmnopqrst uvwxyz’; • T = s(3:6) <enter> Answer?
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?
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?
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?
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!
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.
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)
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.