startsWith - Determine if strings start with pattern - MATLAB (original) (raw)

Determine if strings start with pattern

Syntax

Description

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

If pat is an array containing multiple patterns, thenstartsWith returns 1 if it finds thatstr starts with any element of pat.

example

TF = startsWith([str](#bu4mljm%5Fsep%5Fbu4l86d-str),[pat](#bu4mljm%5Fsep%5Fbu4l86d-pattern),'IgnoreCase',true) ignores case when determining if str starts withpat.

example

Examples

collapse all

Detect Text at Start of String

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

str = ["abstract.docx","data.tar","code.m"; ... "data-analysis.ppt","results.ptx","summary.ppt"]

str = 2x3 string "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.

pat = "data"; TF = startsWith(str,pat)

TF = 2x3 logical array

0 1 0 1 0 0

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

ans = 2x1 string "data-analysis.ppt" "data.tar"

Detect Starts of Paths Using Pattern

Since R2020b

Create a string array that has references to files, including full paths for local files and URLs for remote ones.

str = ["C:\Temp\MyReport.docx"; "D:\Data\Experiment1\Trial1\Sample1.csv"; "https://example.com/Slides.pptx"]

str = 3x1 string "C:\Temp\MyReport.docx" "D:\Data\Experiment1\Trial1\Sample1.csv" "https://example.com/Slides.pptx"

To find the paths that start with a drive letter, create a pattern that matches one letter followed by a colon.

pat = lettersPattern(1) + ":"

pat = pattern Matching:

lettersPattern(1) + ":"

Determine which elements of str start with that pattern. The pattern pat does not match "https:" because lettersPattern(1) matches only one letter.

TF = 3x1 logical array

1 1 0

Display the matching file names.

ans = 2x1 string "C:\Temp\MyReport.docx" "D:\Data\Experiment1\Trial1\Sample1.csv"

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

Test Start of String Against Multiple Substrings

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

str = ["abstract.docx","data.tar.gz","mycode.m","results.ptx"]

str = 1x4 string "abstract.docx" "data.tar.gz" "mycode.m" "results.ptx"

pat = ["abstract","data"]; TF = startsWith(str,pat)

TF = 1x4 logical array

1 1 0 0

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

ans = 1x2 string "abstract.docx" "data.tar.gz"

Ignore Case When Testing Start of String

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

str = ["DATA.TAR.GZ","data.xlsx","SUMMARY.PPT","tmp.gz"]

str = 1x4 string "DATA.TAR.GZ" "data.xlsx" "SUMMARY.PPT" "tmp.gz"

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

TF = 1x4 logical array

1 1 0 0

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

ans = 1x2 string "DATA.TAR.GZ" "data.xlsx"

Determine If Character Vector Starts with Substrings

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

chr = 'data-analysis.ppt'

chr = 'data-analysis.ppt'

TF = startsWith(chr,'data')

TF = startsWith(chr,'test')

Input Arguments

collapse all

str — Input text

string array | character vector | cell array of character vectors

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

pat — Search pattern

string array | character vector | cell array of character vectors | pattern array (since R2020b)

Search pattern, specified as one of the following:

Extended Capabilities

Tall Arrays

Calculate with arrays that have more rows than fit in memory.

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

C/C++ Code Generation

Generate C and C++ code using MATLAB® Coder™.

Usage notes and limitations:

Thread-Based Environment

Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool.

This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.

Distributed Arrays

Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.

This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).

Version History

Introduced in R2016b