idx = 1×2
3 14
Create a string array that contains many names. Determine which names contain the
substring Ann. The contains function returns a logical array that has a 1 wherever str
has an element that contains Ann. To create a new string array that includes only the
matches, index into str with TF.
str = ["Rosemary Ann Jones","Peter Michael Smith","Ann Marie Young"]
str = 1x3 string array
"Rosemary Ann Jones" "Peter Michael Smith" "Ann Marie Young"
TF = contains(str,"Ann")
TF = 1x3 logical array
1 0 1
matches = str(TF)
matches = 1x2 string array
"Rosemary Ann Jones" "Ann Marie Young"
Find the strings that begin with Ann.
TF = startsWith(str,"Ann");
matches = str(TF)
matches =
"Ann Marie Young"
Similarly, the endsWith function find strings that end with a specified piece of text.
You can also use the contains, startsWith, and endsWith functions to determine
whether character vectors contains text.
chr = 'John Paul Jones'
chr =
'John Paul Jones'
Search and Replace Text