Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Programming»JavaScript»How to check if string contains substring in JavaScript

    How to check if string contains substring in JavaScript

    By RahulMay 10, 20202 Mins Read

    The JavaScript indexOf() method search for the substring in string and returns the position of the first occurrence of a specified substring. If substring not found, it returns -1. So you can use JavaScript IndexOf() method to check if a string contains substring in it. In this tutorial we will also provides example to use JavaScript includes() and search() methods to find substring in another string.

    Advertisement

    In a comparison of all 3 methods describes below, indexOf() can be better due to its speed of execution. If no speed matters, you can use any of the following methods.

    Using indexOf() Method

    JavaScript indexOf() method will return the starting position number substring in the main string. If substring not found, it will return -1 as a result.

    1
    2
    3
    4
    var str = "Hello world";
    var substr = "World";
    var result = str.indexOf(substr) > -1;
    alert(result);

    Result will be as below:

     true 
    
    • How to Append Element to Array in JavaScript

    2. Using includes() Method:

    Also you could use JavaScript includes() method. It returns true on match found and false if no match found. Use below sample code to test this:

    1
    2
    3
    4
    var str = "Hello world";
    var substr = "World";
    var result = str.includes(substr) > -1;
    alert(result);

    Result will be as below:

     true 
    

    3: Using search() Method

    You could also use the JavaScript search() method. It returns string position number if match found. If not match found returns -1.

    1
    2
    3
    4
    var str = "Hello World";
    var expr = "/World/";
    var result = str.search(expr) > -1;
    alert(result);

    Result will be as below:

     true 
    
    • >How to Remove Array Element by Value in JavaScript
    inclues indexof javastring position search string substring
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    How to Generate Random String in Python

    How to Generate Random String in Python

    How to Compare Strings in Bash

    Comparing Strings in Bash: A hands-on Tutorial

    How to Convert String to Lowercase in Bash

    Convert String to Lowercase in Bash – Easier Than You Think

    View 3 Comments

    3 Comments

    1. Rey on October 8, 2019 1:08 am

      also var expr = “/World/”; is written incorrectly.

      it should have been written var expr = /World/; to achieve the expected results

      Reply
    2. Rey on October 8, 2019 12:54 am

      var result = str.includes(substr) > -1; is written wrong… will return also true when substr is not in str, as false > -1 evaluates to true!!

      should have been written
      var result = str.includes(substr); // result will return true if substr in str, and will return false if not

      Reply
    3. John on February 10, 2016 9:53 am

      You can also very easily do this by using this code, implemented in modernizr lib:

      function contains(str, substr) {
      return !!~(” + str).indexOf(substr);
      }

      U can see the full text, docs, usage examples and more at cocycles:

      Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • How to List Manually Installed Packages in Ubuntu & Debian
    • 10 Bash Tricks Every Developer Should Know
    • How to Validate Email Address in JavaScript
    • Firewalld: Common Firewall Rules and Commands
    • 12 Apk Commands in Alpine Linux Package Management
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.