sonnets = strip(sonnets);
sonnets(1:10)
ans = 10x1 string array
"THE SONNETS"
"by William Shakespeare"
"I"
"From fairest creatures we desire increase"
"That thereby beauty's rose might never die"
"But as the riper should by time decease"
"His tender heir might bear his memory"
"But thou contracted to thine own bright eyes"
"Feed'st thy light's flame with self-substantial fuel"
"Making a famine where abundance lies"
Split sonnets into a string array whose elements are individual words. You can use the
split function to split elements of a string array on whitespace characters, or on
delimiters that you specify. However, split requires that every element of a string array
must be divisible into an equal number of new strings. The elements of sonnets have
different numbers of spaces, and therefore are not divisible into equal numbers of strings.
To use the split function on sonnets, write a for-loop that calls split on one element
at a time.
Create the empty string array sonnetWords using the strings function. Write a for-loop
that splits each element of sonnets using the split function. Concatenate the output
from split onto sonnetWords. Each element of sonnetWords is an individual word
from sonnets.
sonnetWords = strings(0);
for i = 1:length(sonnets)
sonnetWords = [sonnetWords ; split(sonnets(i))];
end
sonnetWords(1:10)
ans = 10x1 string array
"THE"
"SONNETS"
"by"
"William"
"Shakespeare"
"I"
"From"
"fairest"
6 Characters and Strings