Get IndexOf string in Javascript

string indexof in javascript
In JavaScript strings IndexOf() function is used to find position of specified character in the string. When IndexOf() method is used in javascript it returns position of first occurrence of the given character. If specified character is not found in string then it returns –1 value.

Syntax for IndexOf()


string.indexOf(searchstring, start)

Here search string is the character that is to be find and start is the position from which search to begin.

Example of IndexOf() Method [JavaScript]:


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function GetStringIndex() { 
var str="Javascript IndexOf Method.";
document.write( "a is at position"+ str.indexOf("a") + "<br />");
document.write("IndexOf is at position"+ str.indexOf("IndexOf") + "<br />");
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" value="Submit" onclick="return GetStringIndex();" />
</div>
</form>
</body>
</html>

//Output<
//a is at position 1
//IndexOf is at position 11

Split string JavaScript

split string in javascript
In JavaScript we need to split the string to perform some operations on string. JavaScript uses Split() method to split the given string in JavaScript. Split() method basically split string into array of substrings and generates new array.

String split Syntax:


string.split(separator, limit)

Here separator is a character by which we want to separate string. and limit is to give up to how many number split the string.

Sample to split string [JavaScript]


<script type="text/javascript">
 function splitString()
 {
  var str="Split Javascript String";
  document.write(str.split(" "));
 }
</script> 


//Output
//Split, Javascript, String

In above example of string split, we give space(“ ”) character to split the string with. so it splits the string with “ “ and returns array with substrings.