substr and substring method of the String object in JavaScript

String object in JavaScript method is a series of strings, and similar are defined in a number of ways java class String object, wherein, in the process of a substring from a string taken out in, JavaScript than multi java a substr () method, difference method and the method substring substring method is passed two parameters are taken beginning and end positions of the substring, and the two passed parameters substr method is taken substring starting position and length taken.

The following examples can be seen that, when the element start from 0 substring taken, the result is the same the ASDF, but when started from the first element taken substring, substring is taken to the first position the fourth position is three characters SDF, and substr interception method is taken from the first start position is four characters SDFG:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body>
		<script>
			var strings="ASDFGHJKL";
			console.log(strings.substring(0,4));
			console.log(strings.substr(0,4));
			console.log("-----------------------")
			console.log(strings.substring(1,4));
			console.log(strings.substr(1,4));
		</script>
	</body>
</html>

Published 99 original articles · won praise 93 · views 5224

Guess you like

Origin blog.csdn.net/DangerousMc/article/details/102956886