-1

I have a string and I have to check if that string contains defined substring I need to do some work and otherwise, I should return some error. I have the following code:

function isContains(myString) {
    let subString = 'test1234';
    if(myString.includes(subString)) {
        // to do some work
    } else {
        // return some error.
    }
}

but the problem is if myString = 'my-string-test1-rrr' its condition return true. How can I get true only in case when the whole subString was included in myString?

John
  • 1,375
  • 4
  • 17
  • 40
  • 1
    Cannot reproduce - `'my-string-test1-rrr'.includes('test1234')` returns `false`. Are you using some polyfill for the `includes` method? – VLAZ Sep 19 '19 at 07:57
  • could you show us the code that calls `isContains`? – Thomas Sep 19 '19 at 08:04

3 Answers3

1

Use indexOf() instead.

function isContains(myString) {
    let subString = 'test1234';
    if(myString.indexOf(subString) > -1) {
        // to do some work
    } else {
        // return some error.
    }
}
  • 1
    `Use indexOf() instead.` why? – Thomas Sep 19 '19 at 08:24
  • 1
    indexOf() returns the 0-based index of the first appearance of your full string. If it doesn't find it, will return -1 instead – Mario Garcia Sep 19 '19 at 08:25
  • @MarioGarcia and `.includes` returns `true` or `false` if a string is contained in another one. *logically* `.indexOf(substring) > -1` should be exactly equivalent to `.includes(substring)`. If the recommendation is to use `.indexOf`, then that raises the question of why. – VLAZ Sep 19 '19 at 08:44
0

you can use regex to check if that value is present are not;

example 1 without containing the specific string

var test = 'my-string-test1-rrr';

console.log(' test --- ', test.match(/test1234/g))

example 2

contains the specific string

var test = 'my-string-test1234-rrr';

console.log(' test --- ', test.match(/test1234/g))
RamKumar
  • 126
  • 1
  • 10
  • It's even easier to use the `.test` method from RegExp which simply returns a boolean. Although, watch out for [global regexes, as they are stateful](https://stackoverflow.com/questions/1520800/why-does-a-regexp-with-global-flag-give-wrong-results) – VLAZ Sep 19 '19 at 08:17
0

It is highly recommended to use includes() over indexOf() and further indexOf returns the index of the occurrence where you would prefer an immediate answer - false / true if that substring is found inside the searched string.

Your function does exactly what you are asking. I would suggest to isolate the retrieval of this function and make it purer like so, then when you have the return boolean value you could utilize it after to run whatever logic you wish. This way you keep this function pure and separate your concerns better.

I also believe it would be easier for you to debug your issue if you isolate this functions like In the example I provided.

function isContains(myString) {
    let subString = 'test1234';
    let isContains = false;
    if(myString.includes(subString)) {
        isContains = true;
    } else {
        isContains = false;
    }
    return isContains;
}

You could use it like so in a later phase in your code:

const myString = 'my-string-test1-rrr';
let shouldRunOtherLogic = isContains(myString);

if (shouldRunOtherLogic) {
    // to do some work
} else {
   // return some error.
}

Hope I could help, if there's anything further you may need feel free to let me know.

greensin
  • 472
  • 5
  • 8