Switching back and forth between languages can have its pitfalls. There may be something slightly different between the two that continues to trip you up. Accessing parts of a string is a common programming task and one that may trip you up as you go back and forth from JavaScript to C# or VB.NET.
The substr function for extracting part of a string is used in C and C++, while the substring method is supported in C# and VB.NET. They are both available in JavaScript. They all operate on 0-based strings. If you are also using Visual Basic, which is 1-based, this may be an initial stumbling block. Both substr and substring have versions that accept a single argument and return the remainder of the string starting at that index. The two argument versions behave slightly differently. The substr function takes a start index, and the number of characters to extract. The substring function takes a start index and an end index. In isolation, this seems straightforward, but you will undoubtedly be coding in something other than JavaScript part of the time and may find yourself scratching your head trying to remember which language supports which version. JavaScript has them both. The result of running Listing 3-19 (from substrings.htm), where both versions are called with identical arguments, is shown in Figure 3-5.
<!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" >
<head>
<title>substrings</title>
<script type="text/javascript">
var aString = "0123456789";
document.write(aString.substr(2) + "<br />");
document.write(aString.substring(2) + "<br />");
document.write(aString.substr(2, 5) + "<br />");
document.write(aString.substring(2, 5) + "<br />");
</script>
</head>
<body>
</body>
</html>