1

I have strings as below, I am search text order in all variables but I want to return true for string2 and string3 but for string1 should be false.

var string1 = "orders"
var string2 = "orders/pack"
var string3 = "orders/123"
var str = "order"
console.log(string1.includes(str)); //false 
console.log(string2.includes(str)); //true
console.log(string3.includes(str)); //true

11
  • From what I am seeing, all three strings contain the substring order. Is there a typo, or do you want to include some more explanation? Commented Dec 1, 2017 at 6:05
  • I have created a snippet for you and the code works. see if you can reproduce the issue. Commented Dec 1, 2017 at 6:06
  • str is a substring of string1 Commented Dec 1, 2017 at 6:06
  • 1
    This code returns 3 true for me. Commented Dec 1, 2017 at 6:07
  • 1
    from your code snipp logic should be contains the word and should contain (/) in word Commented Dec 1, 2017 at 6:18

3 Answers 3

1

You can make check like this:

console.log( string1.includes(str) && (string1 != str) );
Sign up to request clarification or add additional context in comments.

1 Comment

(string1 != str) will not work, it is matching order with orders!!
1
var string1 = "orders"
var string2 = "orders/pack"
var string3 = "orders/123"
var str = "/"
console.log(string1.includes(str)); //false 
console.log(string2.includes(str)); //true
console.log(string3.includes(str)); //true

Why are you not using '/' instead of 'orders'? Any boundations which we dont know?

Comments

0

you can try like this it will work

        var string1 = "orders"
        var string2 = "orders/pack"
        var string3 = "orders/123"
        var str = "orders/"

        var result = string1.includes(str);
        console.log(result)
        var result = string2.includes(str);
        console.log(result)
        var result = string3.includes(str);
        console.log(result)

4 Comments

How is this different to the OP, other than you've appended an "s" to str?
include will check the given string is present or not. here all the string has order which means all the string has order so that only it returned as true for all the result
The OP doesn't want them all to return true, the first should return false (based on some unspecified criterion).
in my case first case return false only, another case return true

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.