OneCompiler

[Javascript] How can I check if a string is ends with another string?

I want to check if a string is ended with a particular string, for example, I want to check if a string ends with *. How can I do that in Javascript?

2 Answers

4 years ago by

I tried using endswith() method which worked for me.

4 years ago by Jahaan

Use either endsWith() or extract the substring at the end and match with the substring as shown below

let str = "Hello world*";
let endString ='*'
console.log(str.endsWith(endString));

console.log(str.substr(str.length - endString.length) === endString);

Run here https://onecompiler.com/javascript/3xns4jsw8

4 years ago by Meera