including an empty string, using double quotes. TF is a logical vector that contains a true
value wherever sonnets contains a string with zero characters. Index into sonnets with
TF and delete all strings with zero characters.
TF = (sonnets == "");
sonnets(TF) = [];
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,"
Replace some punctuation marks with space characters. For example, replace periods,
commas, and semi-colons. Keep apostrophes because they can be part of some words in
the Sonnets, such as light's.
p = [".","?","!",",",";",":"];
sonnets = replace(sonnets,p," ");
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 "
Strip leading and trailing space characters from each element of sonnets.
Analyze Text Data with String Arrays