Main Content

endsWith

R2026b

Determine if strings or category names end with pattern

Description

TF = endsWith(str,pat) returns 1 (true) if str ends with the specified pattern, and returns 0 (false) otherwise.

If pat is an array containing multiple patterns, then endsWith returns 1 if it finds that str ends with any element of pat.

example

TF = endsWith(str,pat,IgnoreCase=true) ignores case when determining if str ends with pat.

example

Examples

collapse all

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 = 2×3 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 = 2×3 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 = 2×1 string
    "data.tar.gz"
    "temp-archive.gz"

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 = 2×3 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 = 2×3 logical array

   1   1   0
   1   1   0

Display the matching names.

str(TF)
ans = 4×1 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 = 2×1 string
    "data-analysis.ppt"
    "results.ptx"

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

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 = 1×4 string
    "data.tar.gz"    "mycode.m"    "outputs.xlsx"    "results.pptx"

pat = [".docx",".xlsx",".gz"];
TF = endsWith(str,pat)
TF = 1×4 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 = 1×2 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 = 1×4 string
    "DATA.TAR.GZ"    "mycode.m"    "SUMMARY.PPT"    "tmp.gz"

pattern = ".gz";
TF = endsWith(str,pattern,IgnoreCase=true)
TF = 1×4 logical array

   1   0   0   1

Display the file names that end with .gz. Index back into str using TF.

str(TF)
ans = 1×2 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'
chr = 
'MyLatestPaper.docx'
TF = endsWith(chr,'docx')
TF = logical
   1

TF = endsWith(chr,'gz')
TF = logical
   0

Since R2026b

Load data on electric utility outages in the United States as a table. Convert the Region variable to a categorical array.

T = readtable("outages.csv");
T.Region = categorical(T.Region);

Determine if the category names of Region end with "West". The endsWith function returns a logical array indicating which elements in the Region categorical array have category names that end with "West".

TF = endsWith(T.Region,"West",IgnoreCase=true)
TF = 1468×1 logical array

   1
   0
   0
   1
   1
   1
   1
   1
   0
   1
   0
   1
   0
   0
   1
      ⋮

Display only the rows of the table where the region ends with "West".

T(TF,:)
ans = 522×6 table
     Region         OutageTime        Loss     Customers     RestorationTime            Cause       
    _________    ________________    ______    __________    ________________    ___________________

    SouthWest    2002-02-01 12:18    458.98    1.8202e+06    2002-02-07 16:50    {'winter storm'   }
    West         2004-04-06 05:44    434.81    3.4037e+05    2004-04-06 06:10    {'equipment fault'}
    MidWest      2002-03-16 06:18    186.44    2.1275e+05    2002-03-18 23:23    {'severe storm'   }
    West         2003-06-18 02:49         0             0    2003-06-18 10:54    {'attack'         }
    West         2004-06-20 14:39    231.29           NaN    2004-06-20 19:16    {'equipment fault'}
    West         2002-06-06 19:28    311.86           NaN    2002-06-07 00:51    {'equipment fault'}
    MidWest      2004-09-27 11:09    286.72         66104    2004-09-27 16:37    {'equipment fault'}
    West         2004-05-21 21:45    159.99           NaN    2004-05-22 04:23    {'equipment fault'}
    West         2003-11-12 06:12    254.09    9.2429e+05    2003-11-17 02:04    {'winter storm'   }
    West         2004-12-21 18:50    112.05     7.985e+05    2004-12-29 03:46    {'winter storm'   }
    West         2002-12-16 13:43    70.752    4.8193e+05    2002-12-19 09:38    {'winter storm'   }
    MidWest      2005-02-04 08:18       NaN           NaN    2005-02-04 19:51    {'attack'         }
    MidWest      2002-03-26 01:59    388.04    5.6422e+05    2002-03-28 19:55    {'winter storm'   }
    SouthWest    2004-06-06 05:27    559.41      2.19e+05    2004-06-06 05:55    {'equipment fault'}
    West         2005-06-29 08:37    601.13         32005    2005-06-29 08:57    {'equipment fault'}
    West         2003-04-14 07:11    276.41        1.5647    2003-04-14 08:52    {'equipment fault'}
      ⋮

Input Arguments

collapse all

Input array, specified as a string array, character vector, cell array of character vectors, or categorical array (since R2026b).

Search pattern, specified as one of the following:

  • String array

  • Character vector

  • Cell array of character vectors

  • pattern array

Extended Capabilities

expand all

Version History

Introduced in R2016b

expand all