Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

checking if a string contains a subString or part from subString in typeScript angular 6

i want to know the best way to check if a string contains a subString or part from subString in typeScript ?

for example i have a string path : "home/jobs/AddJobs" And i want to check if it's equal or it contains : "home/jobs"

how can i do that in Angular 6 typescript ?

like image 327
Java.net Avatar asked Jul 14 '26 05:07

Java.net


1 Answers

There is a traditional and modern answer to this question:

const path = 'home/jobs/AddJobs';

// Traditional
if (path.indexOf('home/jobs') > -1) {
    console.log('It contains the substring!');
}

// Modern
if (path.includes('home/jobs')) {
    console.log('It includes the substring!');
}

string.prototype.includes is available in ECMAScript 2015 and newer. If you target lower versions, indexOf works or you can use the MDN Polyfill.

includes is functionally the same as indexOf but naturally returns a boolean value, so you don't need to write the > -1 condition.

like image 153
Fenton Avatar answered Jul 15 '26 22:07

Fenton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!