JavaScript tutorial-String object substr method


JavaScript String object: substr method

The substr(start_pos,[number_chars]) and substring(start_pos,[end_pos]) are used to take out part of a string. These methods produce the same result. However, The second optional arguments of the two methods are different. The second argument of the substr() method is the number of characters to be taken out. The second parameter of the substring() method is the end position of character to be taken out. If these arguments are not specified all characters will be taken out.

Example:
<html>
<head>
<title>Using substr()and substring() method</title>

<script language="JavaScript" type="text/javascript">

var Mystr="I like JavaScript.";
var str1=Mystr.substr(0,3);
var str2=Mystr.substring(0,3);
document.write(str1+"<br>");
document.write(str2+"<br>");

</script>

</head>
<body>
</body>
</html>




Comments




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.