Borrar filtros
Borrar filtros

Find string ends with xls or xlsx

3 visualizaciones (últimos 30 días)
chlor thanks
chlor thanks el 13 de Jul. de 2016
Comentada: chlor thanks el 14 de Jul. de 2016
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.

Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 13 de Jul. de 2016
Editada: Azzi Abdelmalek el 13 de Jul. de 2016
str='abc.xlsx xls.m df.xls er.doc sd.xls'
out=regexp(str,'\S+\.xlsx?\>','match')
  6 comentarios
Guillaume
Guillaume el 14 de Jul. de 2016
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 el 14 de Jul. de 2016
Thank you both! Especially for the notes Guillaume I really appreciate it!

Iniciar sesión para comentar.

Más respuestas (1)

Star Strider
Star Strider el 13 de Jul. de 2016
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’.
  2 comentarios
chlor thanks
chlor thanks el 13 de Jul. de 2016
Editada: chlor thanks el 13 de Jul. de 2016
Thank you for sharing Star!
Star Strider
Star Strider el 13 de Jul. de 2016
My pleasure!

Iniciar sesión para comentar.

Categorías

Más información sobre String Parsing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by