Determine if strings end with pattern
TF = endsWith(str,pattern)
TF = endsWith(str,pattern,'IgnoreCase',true)
example
TF = endsWith(str,pattern) returns 1 (true) if str ends with the specified pattern, and returns 0 (false) otherwise.
str
pattern
1
true
0
false
If pattern is an array containing multiple patterns, then endsWith returns 1 if it finds that str ends with any element of pattern.
endsWith
TF = endsWith(str,pattern,'IgnoreCase',true) ignores case when determining if str ends with pattern.
collapse all
Create a string array that contains file names. Determine which file names end with the .gz extension.
.gz
Starting in R2017a, you can create strings using double quotes.
str = ["abstract.docx","data.tar.gz","mycode.m"; ... "data-analysis.ppt","results.ptx","temp-archive.gz"]
str = 2x3 string "abstract.docx" "data.tar.gz" "mycode.m" "data-analysis.ppt" "results.ptx" "temp-archive.gz"
Return a logical array where the position of each element equal to 1 corresponds to the position of a string in str that ends with .gz.
pattern = ".gz"; TF = endsWith(str,pattern)
TF = 2x3 logical array 0 1 0 0 0 1
Display the file names that end with .gz. Index back into str using TF.
TF
str(TF)
ans = 2x1 string "data.tar.gz" "temp-archive.gz"
Create a string array that contains file names. Determine which file names end with the .docx, .xlsx, or .gz extensions.
.docx
.xlsx
str = ["data.tar.gz","mycode.m","outputs.xlsx","results.pptx"]
str = 1x4 string "data.tar.gz" "mycode.m" "outputs.xlsx" "results.pptx"
pattern = [".docx",".xlsx",".gz"]; TF = endsWith(str,pattern)
TF = 1x4 logical array 1 0 1 0
Display the file names that end with .docx, .xlsx, or .gz. Index back into str using TF.
ans = 1x2 string "data.tar.gz" "outputs.xlsx"
Create a string array that contains file names. Determine which file names end with the .gz extension, ignoring case.
str = ["DATA.TAR.GZ","mycode.m","SUMMARY.PPT","tmp.gz"]
str = 1x4 string "DATA.TAR.GZ" "mycode.m" "SUMMARY.PPT" "tmp.gz"
pattern = ".gz"; TF = endsWith(str,pattern,'IgnoreCase',true)
TF = 1x4 logical array 1 0 0 1
ans = 1x2 string "DATA.TAR.GZ" "tmp.gz"
Create a character vector that contains the name of a file. Determine whether the name ends with specified extensions.
chr = 'MyLatestPaper.docx'
TF = endsWith(chr,'docx')
TF = logical 1
TF = endsWith(chr,'gz')
TF = logical 0
Input text, specified as a string array, a character vector, or a cell array of character vectors.
Search pattern, specified as a string array, a character vector, or a cell array of character vectors.
This function fully supports tall arrays. For more information, see Tall Arrays.
Usage notes and limitations:
str and pattern must be a string scalar, a character vector, or a cell array containing not more than one character vector.
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
contains | find | regexp | startsWith | strcmp | strfind
contains
find
regexp
startsWith
strcmp
strfind
You have a modified version of this example. Do you want to open this example with your edits?