ตัวอย่างที่ให้ไว้ด้านบนมีเอาต์พุตเหมือนกัน แต่ใช่ วิธีการนั้นแตกต่างกัน โดยพารามิเตอร์ก็เช่นกัน
เมธอด substring() ที่พารามิเตอร์ตัวที่สองคือดัชนีเพื่อหยุดการค้นหา ในขณะที่พารามิเตอร์ตัวที่สองของ substr() คือความยาวสูงสุดที่จะส่งคืน
ตัวอย่าง
มาดูตัวอย่างสำหรับ substr () วิธีการใน JavaScript -
<html>
<head>
<title>JavaScript String substr() Method</title>
</head>
<body>
<script>
var str = "Apples are round, and apples are juicy.";
document.write("(1,2): " + str.substr(1,2));
document.write("<br />(-2,2): " + str.substr(-2,2));
document.write("<br />(1): " + str.substr(1));
document.write("<br />(-20, 2): " + str.substr(-20,2));
document.write("<br />(20, 2): " + str.substr(20,2));
</script>
</body>
</html> ตัวอย่าง
มาดูตัวอย่างสำหรับ substring() วิธีการใน JavaScript -
<html>
<head>
<title>JavaScript String substring() Method</title>
</head>
<body>
<script>
var str = "Apples are round, and apples are juicy.";
document.write("(1,2): " + str.substring(1,2));
document.write("<br />(0,10): " + str.substring(0, 10));
document.write("<br />(5): " + str.substring(5));
</script>
</body>
</html>