contains

Tests whether text contains a given piece of text or pattern.

Category: Language Fundamentals > Data Types

Syntax

tf = contains(str, pat)

True where str contains pat.

tf = contains(str, pat, 'IgnoreCase', true)

Ignores letter case.

Description

The search is by plain text, not by regular expression. When pat is a list, an element of str counts as a match if it contains any item of the list. For a single char vector the result is a scalar.

Use startsWith and endsWith for the ends of the text, count to know how many times it occurs, and regexp for regular expressions.

Input arguments

str
Character vector, string array or cell array of character vectors.
pat
Text, a list of text (true if any matches), or a pattern object.
'IgnoreCase'
Logical, default false.

Output arguments

tf
Logical array with the size of str.

Example

Strings, lists and case

contains("hello", 'ell')
contains({'apple','berry'}, 'AP', 'IgnoreCase', true)
contains("abc", ["x" "c"])
Output
ans = logical
   1
ans = 1×2 logical array

   1   0
ans = logical
   1

See also

startsWith endsWith strfind count regexp