C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Argument 1: The index of the first char we are taking a substring at—the first char in a string is at index 0.
Argument 2: The last index of the substring. This is not a count, but rather another position in the string.
JavaScript program that uses substring
var value = "cat123";
console.log("VALUE: " + value);
// Take a substring of the first 3 characters.
// ... Start at index 0.
// ... Continue until index 3.
var result = value.substring(0, 3);
console.log("SUBSTRING: " + result);
Output
VALUE: cat123
SUBSTRING: cat
Length: For substring with 1 argument, the remaining part of the string (after the index specified) is included.
JavaScript program that uses substring, 1 argument
var value = "x_frog";
// Skip over first 2 characters.
var result = value.substring(2);
console.log("VALUE: " + value);
console.log("SUBSTRING: " + result);
Output
VALUE: x_frog
SUBSTRING: frog