String indexOf()
indexOf() searches a string and return the position. indexOf() returns -1 if the substring isn't found.
indexOf() method does the search at the beginning.
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<script type="text/javascript">
var stringValue = "hello world";
document.writeln(stringValue.indexOf("o")); //4
</script>
</head>
<body>
</body>
</html>
An optional second argument tells where to start with.
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<script type="text/javascript">
var stringValue = "hello world";
document.writeln(stringValue.indexOf("o", 6)); //7
</script>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<script type="text/javascript">
var stringValue = "Lorem ipsum dolor sit amet, consectetur adipisicing elit";
var positions = new Array();
var pos = stringValue.indexOf("e");
while(pos > -1){
positions.push(pos);
pos = stringValue.indexOf("o", pos + 1);
}
document.writeln(positions); //3,13,15,29
</script>
</head>
<body>
</body>
</html>
Home
JavaScript Book
Essential Types
JavaScript Book
Essential Types
String:
- The String Type
- String length property
- String charAt()
- String charCodeAt()
- String concat()
- String slice()
- String substr()
- String substring()
- String indexOf()
- String lastIndexOf()
- String trim() Method
- String toLowerCase()
- String toLocaleLowerCase()
- String toUpperCase()
- String toLocaleUpperCase()
- String match()
- String search()
- String replace()
- String split()
- String localeCompare() Method
- String fromCharCode() Method
- String HTML Methods