Find string ends with xls or xlsx

3 次查看(过去 30 天)
I have a char array of strings (each string is a file name) and I would like to find out the strings that ends with xls or xlsx. I know that strcmp can be used only if to compare the first few characters, which is the opposite of what I intent to do---compare the last few characters.
What should I do in this case? Thank you.

采纳的回答

Azzi Abdelmalek
Azzi Abdelmalek 2016-7-13
编辑:Azzi Abdelmalek 2016-7-13
str='abc.xlsx xls.m df.xls er.doc sd.xls'
out=regexp(str,'\S+\.xlsx?\>','match')
  6 个评论
Guillaume
Guillaume 2016-7-14
I would use this simpler regex:
regexp(str, '\.xlsx?$', 'match', 'once')
Matches '.xls', followed by an optional 'x', followed by the end of the string.
chlor thanks
chlor thanks 2016-7-14
Thank you both! Especially for the notes Guillaume I really appreciate it!

请先登录,再进行评论。

更多回答(1 个)

Star Strider
Star Strider 2016-7-13
The findstr function may do what you want:
fn = 'filename.xlsx';
xls_pos = findstr(fn,'xls')
xls_pos =
10
So if ‘xls_pos’ (in this example) is not empty, the string contains ‘xls’ or ‘xlsx’.

类别

Help CenterFile Exchange 中查找有关 String Parsing 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by