H I O X INDIA
Javascript Tutorial
Google
Web hscripts.com
 HOME  ||  Scripts  ||  Purchase  ||  Tutorials  ||  Images  ||  Tools  ||  Directories 
  :-)  Send Page   :-)   Feedback   :-)   Register   :-)   Links   :-)   Support   :-)   Bookmarks :-)  
 Forums   Hosting   Internet Stats   Easy Calculation   FUN Games 

Java-Script Tutorial
Introduction
Variables
Operators
Statements
Loops
Functions
Objects
Arrays
Browser Objects
Form DOM
Ask Your Doubts
Feedback





String Function - indexOf


Topic

how to find the position of a character or word in a string using javascript?



Explanation

Object: String

Method or Function: indexOf(string)

Description: This method takes a character or string as an argument. Searches the given string in the main string and returns the index or starting (first occurrence) position of the string. Returns -1 if the search string is not present in the main string.


Example Code:
<script language=javascript>
var ss = "a string index of test ";
var result = ss.indexOf("ri");
document.write(result);
</script>

Result:

Explanation:
In the above example,
  • the main string is stored in a variable ss.
  • the method takes a string "ri" as argument.
  • this function searches in main string (ss) for the search string (ri).
  • the first occurrence of the search word is found at the position 4 and is returned as the result.



  •         Javascript (JS) is one of the most used languages in the world. Some times spelled as Java Script. Hope you enjoy this tutorial. Do send your feedback or suggestions on this javascript or java script tutorial. This is a copyright content.

    others

    privacy policy     license     sitemap
    © 2004-2005 HIOX INDIA - hioxindia.com

    Other Links