A JavaScript string stores a series of characters like "John Doe".
A string can be any text inside double or single quotes:
A string can be any text inside double or single quotes:
<html>
<head>
<title>String functions in javascript</title>
<script language="javascript" type="text/javascript">
document.write("<h1 align='center'><font color='blue'>String Functions</font></h1><hr/>");
var str = "This is sample string";
document.write("<h2>Length of string</h2>");
document.write(str.length);
document.write("<h2>Convert string to upper case</h2>");
document.write(str.toUpperCase());
document.write("<h2>Convert string to lower case</h2>");
document.write(str.toLowerCase());
document.write("<h2>Position of value in string at first occurance</h2>");
document.write(str.indexOf("s"));
document.write("<br/>");
document.write(str.indexOf("S"));
document.write("<h2>Position of value in string at last occurance</h2>");
document.write(str.lastIndexOf("s"));
document.write("<br/>");
document.write(str.lastIndexOf("S"));
document.write("<h2>Match value in string</h2>");
document.write(str.match("sample"));
document.write("<br/>");
document.write(str.match("Sample"));
document.write("<h2>Replace value in string</h2>");
document.write(str.replace("sample","test"));
document.write("<br/>");
document.write(str.replace("Sample","test"));
document.write("<h2>Substring value</h2>");
document.write(str.substring(2,10));
document.write("<br/>");
document.write(str.substring(0));
</script>
</head>
</html>
Output
Copy paste this code into html file and execute yourself in browser
0 comments:
Post a Comment