MrM
MrM

Reputation: 21999

if(string.Contains(string)). Is that possible?

I am trying to check if a string is contained in another string. Code behind is pretty straight forward. How can I do that in jquery?

function deleteRow(locName, locID) {
    if(locName.Contains(locID)) {
        alert("success");
    }
}

Upvotes: 0

Views: 445

Answers (3)

Tim Down
Tim Down

Reputation: 324627

You can use the indexOf method of the string. If you really want the convenience of having a contains method, you could add one to String.prototype:

String.prototype.contains = function(str) {
    return this.indexOf(str) > -1;
};

alert("foobar".contains("oob")); // true
alert("foobar".contains("baz")); // false

Upvotes: 1

Senad Meškin
Senad Meškin

Reputation: 13756

you can use indexOf if the string is found returned result will be different from -1

function deleteRow(locName, locID) {
    if(locName.indexOf(locID) != -1) {
        alert("success");
    }
}

I hope this helps

Upvotes: 0

Raynos
Raynos

Reputation: 169451

Use String.prototype.indexOf

For example: (locName.indexOf(locID) > -1)

String.prototype.contains doesn't exist.

Upvotes: 9

Related Questions