Who is online?  189 guests and 1 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#

Topic RSS Feed

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

Posted: 5/20/2010 3:16:09 AM

Lurker 170  points  Lurker
  • Joined on: 10/17/2009 1:28:08 AM
  • 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


javascript

Posted: 5/21/2010 3:10:57 PM

Professional 10133  points  Professional
  • Joined on: 4/19/2009 1:46:52 AM
  • Posts: 241
answered  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>


 

 


javascript, indexof
Page 1 of 1 (2 items)