1

Is there an equivalent of right() function that I can use in jquery. I want to get records with fileextension xlsx.

 new Guid(context.Documents.Where(T => T.FileName == ".xlsx").Select(T => T.ItemGuid).First().ToString());

something like

 select * from document where right(filename,4) = 'xlsx'

I don't want to store the filename in a variable and later manipulate it. I want to be able to directly use it in my where condition. 'Documents' is my table name and "FileName" is a field that holds names of the file that I upload, now I need to get filter only the files that has the extension 'xlsx'. I tried doing

  guid temp = new Guid(context.Documents.Where(T => T.FileName.Substring(T.FileName.Length - 4) == ".xlsm").Select(T => T.ItemGuid).First().ToString());

but I get the error "Sequence contains no elements" error.

* Update: Used the EndsWith() to get the information I wanted. This works now:

  guid temp = new Guid(context.Documents.Where(T => T.FileName.EndsWith("xlsm")).Select(T => T.ItemGuid).First().ToString());

thanks.

sansid
  • 575
  • 3
  • 10
  • 20

6 Answers6

1

You can use .slice (MDN) function, taking into account that passing a negative value into it makes it cut the string from the end. )

var test = 'filename.xslx';
if (test.slice(-4) === 'xslx') {
  alert("Passed");
}
raina77ow
  • 103,633
  • 15
  • 192
  • 229
1
filename.substr(-4)

Using .substr with a negative index will return a substring from the end of the string.

jackwanders
  • 15,612
  • 3
  • 40
  • 40
0

You might be looking for [name$="value"] selector documented here.

Michal Klouda
  • 14,263
  • 7
  • 53
  • 77
0

There is not, you can use

var filename = "file.xlsx";
var extension = filename.substr(filename.length - 4);

or to get the characters after the dot, you could use:

var extension = filename.split('.').pop();
Rusty Jeans
  • 1,426
  • 9
  • 10
0

right() is the same as endswith()

function endsWith(str, suffix) {
    return str.indexOf(suffix, str.length - suffix.length) !== -1;
}
Community
  • 1
  • 1
SliverNinja - MSFT
  • 31,051
  • 11
  • 110
  • 173
0

Use could javascript match() which provides regex matching.

var filename = 'somefile.xlsx';
var matches = filename.match('/\.xlsx$/i');
if (matches.length > 0) {
    // you have a match
}
Mike Brant
  • 70,514
  • 10
  • 99
  • 103