Main Content

contains

Determine if pattern is in strings

Description

example

TF = contains(str,pat)returns1(true) ifstrcontains the specified pattern, and returns0(false) otherwise.

Ifpatis an array containing multiple patterns, thencontainsreturns1if it finds any element ofpatinstr.

example

TF = contains(str,pat,'IgnoreCase',true)ignores case when determining ifstrcontainspat.

Examples

collapse all

Create a string array of names, where some names containPaul.

Starting in R2017a, you can create strings using double quotes.

str = ["Mary Ann Jones","Paul Jay Burns","John Paul Smith"]
str =1x3 string"Mary Ann Jones" "Paul Jay Burns" "John Paul Smith"

If you are using R2016b, create string arrays using thestringfunction instead of double quotes.

Return a logical array where the position of each element equal to1corresponds to the position of a string instrthat containsPaul.

pat ="Paul"; TF = contains(str,pat)
TF =1x3 logical array0 1 1

Display the strings that containPaul. Index back intostrusingTF.

str(TF)
ans =1x2 string"Paul Jay Burns" "John Paul Smith"

Since R2020b

Create a string array that contains addresses.

str = ["221B Baker St.","Tour Eiffel Champ de Mars","4059 Mt Lee Dr."]
str =1x3 string"221B Baker St." "Tour Eiffel Champ..." "4059 Mt Lee Dr."

To find addresses that contain numbers, create a pattern that matches an arbitrary number of digits by using thedigitsPatternfunction.

pat = digitsPattern
pat =patternMatching: digitsPattern

Return a logical array indicating which strings contain digits. Display the matching strings.

TF = contains(str,pat)
TF =1x3 logical array1 0 1
str(TF)
ans =1x2 string"221B Baker St." "4059 Mt Lee Dr."

Search for strings that have a sequence of digits followed by one letter. You can build more complex patterns by combining simple patterns.

pat = digitsPattern + lettersPattern(1)
pat =patternMatching: digitsPattern + lettersPattern(1)
TF = contains(str,pat); str(TF)
ans = "221B Baker St."

For a list of functions that create pattern objects, seepattern.

Create a string array of names, where some names contain eitherAnnorPaul.

str = ["Mary Ann Jones","Christopher Matthew Burns","John Paul Smith"]
str =1x3 string"Mary Ann Jones" "Christopher Matth..." "John Paul Smith"

Find the elements ofstrthat contain eitherAnnorPaul.

pat = ["Ann","Paul"]; TF = contains(str,pat)
TF =1x3 logical array1 0 1

Index back intostrusingTF.

str(TF)
ans =1x2 string"Mary Ann Jones" "John Paul Smith"

Create a string array that contains names. Determine which names containanne,忽略的情况。

Starting in R2017a, you can create strings using double quotes.

str = ["Anne","Elizabeth","Marianne","Tracy"]
str =1x4 string"Anne" "Elizabeth" "Marianne" "Tracy"
pat ="anne"; TF = contains(str,pat,'IgnoreCase',真正的)
TF =1x4 logical array1 0 1 0

Display the strings that containanne. Index back intostrusingTF.

str(TF)
ans =1x2 string"Anne" "Marianne"

Create a character vector that contains a list of foods. Determine if the names of different foods are in the character vector.

chr ='peppers, onions, and mushrooms'; TF = contains(chr,'onion')
TF =logical1
TF = contains(chr,'pineapples')
TF =logical0

Input Arguments

collapse all

Input text, specified as a string array, character vector, or cell array of character vectors.

Search pattern, specified as one of the following:

  • String array

  • Character vector

  • Cell array of character vectors

  • patternarray(since R2020b)

Extended Capabilities

Introduced in R2016b