<!-- JavaScript:Strings code snippet 3. -->
<!-- Create a 'String' object, call methods using dot notation,
assign variables and write values to the page -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!-- Code source: http://www.justfigures.co.uk/ -->
<!-- A resource for web developers using XHTML, CSS, JavaScript, PHP -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>View code snippet page</title>
</head>
<body>
<script language="JavaScript" type="text/javascript">
<!--
/* create a new 'String' object called 'myString' */
var myString = new String("John.Smith@www.com");
/* call 'String' object methods and assign variables */
document.write("The string we are using as a test is: "+myString+"<br />");
/* 'indexOf()' finds and returns the position in the index at which the
first occurrence of a substring in a string begins */
// note that strings like arrays are indexed from 0
var indexOf1=myString.indexOf("J");
document.write("The index position of the start of substring uppercase 'J' is: "+indexOf1+"<br />");
var indexOf2=myString.indexOf("j");
document.write("The index position of the start of not found substring lowercase 'j' is: "+indexOf2+"<br />");
var indexOf3=myString.indexOf("o");
document.write("The index position of the start of the substring 'o' is: "+indexOf3+"<br />");
var indexOf4=myString.indexOf(".com");
document.write("The index position of the start of the substring '.com' is: "+indexOf4+"<br />");
/* 'substring()' removes a string from another string using the index
position as parameters, accepts one or two parameters, if the second
parameter is not included then to the end of the string is returned */
var substring1=myString.substring(5,9);
document.write("String returned from substring(5,9) is: "+substring1+"<br />");
var substring2=myString.substring(5);
document.write("String returned from substring(5) is: "+substring2+"<br />");
var substring3=myString.substring(myString.indexOf('@'));
document.write("String returned from substring(myString.indexOf('@')) is: "+substring3+"<br />");
var substring4=myString.substring(myString.indexOf('.')+1,myString.indexOf('@'));
document.write("String returned from substring(myString.indexOf('.')+1,myString.indexOf('@')) is: "+substring4+"<br />");
// -->
</script>
</body>
</html>