Loading ...

How to use .contains in Javascript as we have in c#

Who is online?  0 guests and 0 members
home  »  forums   »  asp.net topics   »  client side web development   » How to use .contains in Javascript as we have in c#

How to use .contains in Javascript as we have in c#

Posts under the topic: How to use .contains in Javascript as we have in c#

Posted: 5/20/2010

Lurker 170  points  Lurker
  • Joined on: 10/17/2009
  • Posts: 34

Hi,

I have a string now I need to find whether that string contains a particular word or not but I am not able to find .contains in javascript? Yes I know how to use in c# but not in javascript . Please help


tags javascript

Posted: 5/21/2010

Guru 16518  points  Guru
  • Joined on: 4/19/2009
  • Posts: 483
  Answered

Hi,

Go with indexOf. I have made a sample below for you try it.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">

        var inputString = 'Hello this is a test string'; //test input
        var wordToFound = 'this';
        if (inputString.indexOf(wordToFound) == -1) {
            alert(wordToFound + ' Not Found');
        }
        else {
            alert(wordToFound + ' Found');
        }

        //Now let's do another test 
        wordToFound = 'how';
        if (inputString.indexOf(wordToFound) == -1) {
            alert(wordToFound + ' Not Found');
        }
        else {
            alert(wordToFound + ' Found');
        }
        
   
    </script>
</head>
<body>

</body>
</html>


 

 


Page 1 of 1 (2 items)