startsWith

Determine if string starts with pattern

Syntax

  • TF = startsWith(str,pattern)
    example
  • TF = startsWith(str,pattern,'IgnoreCase',true)
    example

Description

example

TF = startsWith(str,pattern) returns 1 (true) if str starts with the specified pattern, and returns 0 (false) otherwise.

If pattern is an array containing multiple patterns, then startsWith returns 1 if it finds that str starts with any element of pattern.

example

TF = startsWith(str,pattern,'IgnoreCase',true) ignores case when determining if str starts with pattern.

Examples

collapse all

Create a string array that contains file names. Determine which file names start with the word data.

str = string({'abstract.docx','data.tar','code.m';...
            'data-analysis.ppt','results.ptx','summary.ppt'})
str = 

  2×3 string array

    "abstract.docx"        "data.tar"       "code.m"     
    "data-analysis.ppt"    "results.ptx"    "summary.ppt"

Return a logical array where the position of each element equal to 1 corresponds to the position of a string in str that starts with data.

pattern = 'data';
TF = startsWith(str,pattern)
TF =

  2×3 logical array

   0   1   0
   1   0   0

Display the file names that start with data. Index back into str using Tf.

str(TF)
ans = 

  2×1 string array

    "data-analysis.ppt"
    "data.tar"

Create a string array that contains file names. Determine which file names start with either abstract or data.

str = string({'abstract.docx','data.tar.gz','mycode.m','results.ptx'})
str = 

  1×4 string array

    "abstract.docx"    "data.tar.gz"    "mycode.m"    "results.ptx"

pattern = {'abstract','data'};
TF = startsWith(str,pattern)
TF =

  1×4 logical array

   1   1   0   0

Display the strings that start with either abstract or data. Index back into str using TF.

str(TF)
ans = 

  1×2 string array

    "abstract.docx"    "data.tar.gz"

Create a string array that contains file names. Determine which file names start with data, ignoring case.

str = string({'DATA.TAR.GZ','data.xlsx','SUMMARY.PPT','tmp.gz'})
str = 

  1×4 string array

    "DATA.TAR.GZ"    "data.xlsx"    "SUMMARY.PPT"    "tmp.gz"

pattern = 'data';
TF = startsWith(str,pattern,'IgnoreCase',true)
TF =

  1×4 logical array

   1   1   0   0

Display the strings that start with data. Index back into str using TF.

str(TF)
ans = 

  1×2 string array

    "DATA.TAR.GZ"    "data.xlsx"

Create a character vector that contains the name of a file. Determine if the name starts with different patterns.

chr = 'data-analysis.ppt'
chr =

data-analysis.ppt

TF = startsWith(chr,'data')
TF =

  logical

   1

TF = startsWith(chr,'test')
TF =

  logical

   0

Related Examples

Input Arguments

collapse all

Input string.

Data Types: string | char | cell

Search pattern, specified as a string array, a character vector, or a cell array of character vectors.

Data Types: string | char | cell

More About

collapse all

Tall Array Support

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

Introduced in R2016b

Was this topic helpful?