String substring()
substring() methods return a substring and accepts either one or two arguments.
The first argument is the starting position. The second argument tells where to stop(exclusive). If the second argument is omitted, it is default to ending of the string.
substring() does not alter the value of the string itself.
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<script type="text/javascript">
var stringValue = "hello world";
document.writeln(stringValue.substring(3)); //"lo world"
document.writeln(stringValue.substring(3,7)); //"lo w"
</script>
</head>
<body>
</body>
</html>
All negative numbers are converted to 0.
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<script type="text/javascript">
var stringValue = "hello world";
document.writeln(stringValue.substring(-3)); //"hello world"
document.writeln(stringValue.substring(3, -4)); //"hel"
</script>
</head>
<body>
</body>
</html>
Home
JavaScript Book
Essential Types
JavaScript Book
Essential Types
String:
- The String Type
- String length property
- String charAt()
- String charCodeAt()
- String concat()
- String slice()
- String substr()
- String substring()
- String indexOf()
- String lastIndexOf()
- String trim() Method
- String toLowerCase()
- String toLocaleLowerCase()
- String toUpperCase()
- String toLocaleUpperCase()
- String match()
- String search()
- String replace()
- String split()
- String localeCompare() Method
- String fromCharCode() Method
- String HTML Methods