129

I need to check whether a string contains another string or not?

var str1 = "ABCDEFGHIJKLMNOP";
var str2 = "DEFG";

Which function do I use to find out if str1 contains str2?

Quentin
  • 62,093
  • 7
  • 131
  • 191
diggersworld
  • 12,770
  • 24
  • 84
  • 119
  • a **benchmark** of different solutions can be found here: http://jsben.ch/#/RVYk7 – EscapeNetscape Nov 11 '16 at 10:24
  • @EscapeNetscape: Nice to see a benchmark link. If I change the observation string to a very long `UTF-8` multibyte text, the results change drastically? Also, the results changed with change in position of substring in the observation string. BTW, which one were you suggesting. – Fr0zenFyr Feb 21 '19 at 12:27

5 Answers5

290

You can use javascript's indexOf function.

var str1 = "ABCDEFGHIJKLMNOP";
var str2 = "DEFG";
if(str1.indexOf(str2) != -1){
    console.log(str2 + " found");
}
Jonathan
  • 2,700
  • 4
  • 23
  • 41
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • 4
    this would works only when substring has exact match, will not work around "DEfG" as f is small case. – Red Swan Jul 23 '12 at 11:31
  • 10
    @RedSwan: you could convert the string to uppercase or lowercase and then do the comparison, problem solved. – diggersworld Nov 08 '14 at 09:40
  • 1
    [`indexOf()` is not supported in IE < 9](http://stackoverflow.com/q/8848097/339874) – mulllhausen Oct 11 '16 at 07:19
  • 2
    @mulllhausen: It's 2016, this answer is 6 years old, and most sites have stopped supporting IE < 9. – gen_Eric Oct 11 '16 at 13:18
  • 1
    @RocketHazmat: fair enough. I just commented because it wasn't mentioned in any of the answers on this page and people may be interested to know. – mulllhausen Oct 12 '16 at 02:58
  • 1
    For clarity, you should use === instead of == – A Friend Sep 19 '18 at 01:23
  • @DeepSojitra jQuery is not a language. It's a JavaScript library. The jQuery library does not contain this function, it's part of the JavaScript language. Also this question is 11 years old and this answer was accepted. – gen_Eric Aug 05 '21 at 19:35
  • @RedSwan or you could also use `indexOf("DEfG", StringComparison.OrdinalIgnoreCase);` – Miro Feb 28 '22 at 14:12
17
var str1 = "ABCDEFGHIJKLMNOP";
var str2 = "DEFG";

sttr1.search(str2);

it will return the position of the match, or -1 if it isn't found.

Khaihkd
  • 307
  • 2
  • 6
  • 1
    It would be nice to note that [Array.prototype.search](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/search) implicitly converts the string to a regular expression which may not give the desired result. – Uyghur Lives Matter Mar 13 '15 at 17:55
  • 1
    Last row should be: str1.search(str2); not sttr1, IMHO – Isma Jan 21 '20 at 10:16
10

Please try:

str1.contains(str2)
nstCactus
  • 5,141
  • 2
  • 30
  • 41
scott
  • 149
  • 1
  • 2
9

I use,

var text = "some/String"; text.includes("/") <-- returns bool; true if "/" exists in string, false otherwise.

eaglei22
  • 2,589
  • 1
  • 38
  • 53
5

If you are worrying about Case sensitive change the case and compare the string.

 if (stringvalue.toLocaleLowerCase().indexOf("mytexttocompare")!=-1)
        {

            alert("found");
        }
mzonerz
  • 1,220
  • 15
  • 22