Pages

Monday, June 8, 2015

string contains javascript

string contains javascript

In Java, I have used contains method  of String class, which is used to determine whether a particular string / character sequence has been present in the source String or not.

  • Returns true, if the sequence / string is present .
  • Returns false, if the sequence / string is absent .

I wanted to have such a similar function in Java script as well.
For which the solution is mentioned in the following URL
 
http://www.w3schools.com/jsref/jsref_indexof.asp

var str = "Hello world, welcome to the universe."; // Source String
var n = str.indexOf("welcome"); // Sample Sequence for comparing

The result is 13 ( Start Index position of the word in the main string ) .

If the word is present, this method returns the starting index position of the word in the main string.

If the word is absent, this method returns -1.

if( n == -1 )
 {
 alert( " Sequence absent" );
 }
else
{
 alert(" Sequence Present");
}

No comments:

Post a Comment