Check if a string contains another substring

The easiest way to check if a string contains another string in JavaScript. Also known as substring.


Libraries

Code Snippet
function Contains(bstring, sstring) {
    return bstring.indexOf(sstring) !== -1;
}


Implementation

Use the function above like <code>var contain_ll = Contains("Hello World!", "ll"); var contain_ee = Contains("Hello World!", "ee");</code> In the example the output is going to be <code>contain_ll = true; contain_ee = false;</code>





Comments
  • mode_editWrite a comment



  • forumComments




Embed Code

We provide special containers which include the full snippet and a link to this site. So for example when you make a tutorial and you are using code from this site, you can use one of the following snippet embeddings:

Load Preview Get Code Close Preview







    Share
    Rating
    thumb_up 1 thumb_down
    Author
    Thorolus

    I'm the head developer and co-founder of GlovilGames Studios (glovilgames.com) and other platforms GlovilGames created, like vsnippets.com, worth-calculator.net, python-obfuscator.com and some mobile apps and games.

    Tags
    string
    substring
    contains
    indexOf



    Informations

    posted on 2016-09-12 20:06:59

    viewed 51 times

    snippet's UUID is 57d6eec399988

    Similar Snippets