endsWith
Determine if strings end with pattern
Description
Examples
Detect Text at End of String
Create a string array that contains file names. Determine which file names end with the .gz
extension.
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
.
pat = ".gz";
TF = endsWith(str,pat)
TF = 2x3 logical array
0 1 0
0 0 1
Display the file names that end with .gz
. Index back into str
using TF
.
str(TF)
ans = 2x1 string
"data.tar.gz"
"temp-archive.gz"
Detect File Extensions Using Patterns
Create a string array of file and folder names, where some names have extensions.
str = ["abstract.docx","data.tar.gz","REPORTS"; ... "data-analysis.ppt","results.ptx","ARCHIVE"]
str = 2x3 string
"abstract.docx" "data.tar.gz" "REPORTS"
"data-analysis.ppt" "results.ptx" "ARCHIVE"
To find names that end with extensions, create a pattern that matches a period followed by letters by using the lettersPattern
function. (You can build up a complex pattern by combining simple patterns in expressions. Such expressions can also include literal text, like "."
in this example.)
pat = "." + lettersPattern
pat = pattern
Matching:
"." + lettersPattern
Return a logical array indicating which names end with extensions.
TF = endsWith(str,pat)
TF = 2x3 logical array
1 1 0
1 1 0
Display the matching names.
str(TF)
ans = 4x1 string
"abstract.docx"
"data-analysis.ppt"
"data.tar.gz"
"results.ptx"
Find the names with extensions that are exactly three letters long.
pat = "." + lettersPattern(3);
TF = endsWith(str,pat);
str(TF)
ans = 2x1 string
"data-analysis.ppt"
"results.ptx"
For a list of functions that create pattern objects, see pattern
.
Test End of String Against Multiple Extensions
Create a string array that contains file names. Determine which file names end with the .docx
, .xlsx
, or .gz
extensions.
str = ["data.tar.gz","mycode.m","outputs.xlsx","results.pptx"]
str = 1x4 string
"data.tar.gz" "mycode.m" "outputs.xlsx" "results.pptx"
pat = [".docx",".xlsx",".gz"]; TF = endsWith(str,pat)
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
.
str(TF)
ans = 1x2 string
"data.tar.gz" "outputs.xlsx"
Ignore Case When Testing End of String
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
Display the file names that end with .gz
. Index back into str
using TF
.
str(TF)
ans = 1x2 string
"DATA.TAR.GZ" "tmp.gz"
Determine If Character Vector Ends with Extension
Create a character vector that contains the name of a file. Determine whether the name ends with specified extensions.
chr = 'MyLatestPaper.docx'
chr = 'MyLatestPaper.docx'
TF = endsWith(chr,'docx')
TF = logical
1
TF = endsWith(chr,'gz')
TF = logical
0
Input Arguments
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:
String array
Character vector
Cell array of character vectors
pattern
array (since R2020b)
Extended Capabilities
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
The
endsWith
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:
str
andpat
must be a string scalar, a character vector, or a cell array containing not more than one character vector.
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
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)