Saturday, 24 July 2021

JavaScript tutorial #9 | JavaScript Search text string method @echocoding | soham kawde

 


free source code :- content of table is indexOf( ) string method lastIndexOf( ) string method search( ) string method Difference between indexOf and search includes( )string method startWith( ) endWith( ) indexOf( ) string method : - This javaScript string method get the position of given word's first occurrence. Example of indexOf( ) :- If given text is "My name is soham"; and You are searching for "name" in the indexOf method. You will get 3 in result. Because in "name" , n is first character so the position of n is result position number of indexOf( ). method. Note: IndexOf( ) search and count from left to right and return position of first occurrence only. lastIndexOf( ) string method : - This JavaScript string method return the position of last occurrence but it count same as indexOf( ) method ( left to right ) and search from Right to left . Example :- Given text is "This glass of water has no water." search for "water" in given text you will get 25 position number for water. from Right side to left side lastIndexOf( ) start searching and from left side to right side it start counting. Hence result is 25 What happens if searching text like name and water is not exist in given text ? IndexOf and lastIndexOf method return -1 for no match. Difference between indexOf( ) and search( ) IndexOf() method get no regular expression. search() method has no second parameter to pass. includes() : - return true for match occurrence and false for no match. startWith() :- Return true if the text is start with searching text with is passed in startWith method else return false. endWith( ) : - return true if the text is end with searching text with is passed in endWith() method else return false.



<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>

    <script>
        var text = "this is programming coding programming";
        var x = text.indexOf('programming',10);
        // document.write(x);
        var y = text.lastIndexOf('programming',20);
        // document.write(y);

        var yl = text.lastIndexOf('otherword');
        // document.write(yl);
        var s = text.search('programming');
        // document.write(s);
        var inc = text.includes('coding');
        // document.write(inc);
        var start = text.startsWith('is',5);
        // document.write(start);
        var end = text.endsWith('programming',19);
        document.write(end);
    </script>
</body>
</html>

No comments:

By laptop