contains - Determine if pattern is in strings - MATLAB (original) (raw)

Determine if pattern is in strings

Syntax

Description

TF = contains([str](#bu4l86d%5Fsep%5Fbu4l86d-str),[pat](#bu4l86d%5Fsep%5Fbu4l86d-pattern)) returns 1 (true) if str contains the specified pattern, and returns 0 (false) otherwise.

If pat is an array containing multiple patterns, thencontains returns 1 if it finds any element of pat in str.

If str is a string array or cell array, thenTF is a logical array that is the same size asstr.

example

TF = contains([str](#bu4l86d%5Fsep%5Fbu4l86d-str),[pat](#bu4l86d%5Fsep%5Fbu4l86d-pattern),'IgnoreCase',true) ignores case when determining if str containspat.

example

Examples

collapse all

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

You can create strings using double quotes.

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

str = 1×3 string "Mary Ann Jones" "Paul Jay Burns" "John Paul Smith"

Return a logical array where the position of each element equal to 1 corresponds to the position of a string in str that contains Paul.

pat = "Paul"; TF = contains(str,pat)

TF = 1×3 logical array

0 1 1

Display the strings that contain Paul. Index back into str using TF.

ans = 1×2 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 = 1×3 string "221B Baker St." "Tour Eiffel Champ de Mars" "4059 Mt Lee Dr."

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

pat = pattern Matching:

digitsPattern

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

TF = 1×3 logical array

1 0 1

ans = 1×2 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 = pattern Matching:

digitsPattern + lettersPattern(1)

TF = contains(str,pat); str(TF)

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

Create a string array of names, where some names contain either Ann or Paul.

str = ["Mary Ann Jones","Christopher Matthew Burns","John Paul Smith"]

str = 1×3 string "Mary Ann Jones" "Christopher Matthew Burns" "John Paul Smith"

Find the elements of str that contain either Ann or Paul.

pat = ["Ann","Paul"]; TF = contains(str,pat)

TF = 1×3 logical array

1 0 1

Index back into str using TF.

ans = 1×2 string "Mary Ann Jones" "John Paul Smith"

Create a string array that contains names. Determine which names contain anne, ignoring case.

You can create strings using double quotes.

str = ["Anne","Elizabeth","Marianne","Tracy"]

str = 1×4 string "Anne" "Elizabeth" "Marianne" "Tracy"

pat = "anne"; TF = contains(str,pat,'IgnoreCase',true)

TF = 1×4 logical array

1 0 1 0

Display the strings that contain anne. Index back into str using TF.

ans = 1×2 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 = contains(chr,'pineapples')

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:

Extended Capabilities

expand all

Thecontains function fully supports tall arrays. For more information, see Tall Arrays.

Usage notes and limitations:

Version History

Introduced in R2016b