startsWith

Tests whether text begins with a given prefix.

Category: Language Fundamentals > Data Types

Syntax

tf = startsWith(str, pat)

Returns true where str begins with pat.

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

Compares without regard to letter case.

Description

The comparison is made character by character from the start of each text. An empty prefix always matches. str can be a collection, so one call checks a whole list of names.

The prefix is literal text, not a pattern: characters such as * or . have no special meaning.

Input arguments

str
String array, character vector, or cell array of character vectors.
pat
Prefix to look for, or several prefixes. If any of them matches, the result is true.
'IgnoreCase'
Logical flag, false by default.

Output arguments

tf
Logical array with one element per text in str.

Example

Single text, list and case

startsWith("hello world", "hello")
startsWith({'apple','banana','avocado'}, 'a')
startsWith('ABC', 'ab', 'IgnoreCase', true)
Output
ans = logical
   1
ans = 1×3 logical array

   1   0   1
ans = logical
   1

See also

endsWith contains strncmp strfind extractBefore