Home  >  Article  >  Web Front-end  >  What is the usage of substr function in jquery

What is the usage of substr function in jquery

青灯夜游
青灯夜游Original
2022-03-22 19:20:103233browse

In jquery, the substr function is used to intercept a substring of a specified number of characters starting from the specified subscript of a string. This function supports two parameters. The first parameter specifies the starting subscript for interception. The two parameters specify the number of characters; the syntax is "string object.substr(start,length)".

What is the usage of substr function in jquery

The operating environment of this tutorial: windows7 system, jquery1.10.2 version, Dell G3 computer.

In jquery, the substr function is used to intercept a specified number of characters starting from the specified subscript of a string. This intercepted character can be called a "substring" or "substring". Its syntax The format is as follows:

stringObject.substr(start,length)
##ParameterDescriptionRequired. The starting index of the substring to be extracted. Must be a numeric value. If negative, this parameter declares the position from the end of the string. That is, -1 refers to the last character in the string, -2 refers to the second to last character, and so on. Optional. The number of characters in the substring. Must be a numeric value. If this parameter is omitted, the string from the beginning to the end of
start
length stringObject is returned.
Return value:

A new string containing length characters starting from the start of stringObject (including the character pointed to by start). If length is not specified, the returned string contains characters from start to the end of the stringObject.

Example: intercept the string "Hello world!" starting from subscript 5, intercept 6 characters


<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<script src="js/jquery-1.10.2.min.js"></script>
		<script>
			$(document).ready(function() {
				var text=$("p").text();
				console.log(text.substr(5,6));
			});
		</script>
	</head>
	<body>

		<p>Hello world!</p>

	</body>
</html>

What is the usage of substr function in jquery

[Recommended learning:

jQuery video tutorialweb front-end

The above is the detailed content of What is the usage of substr function in jquery. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Is gulp based on node?Next article:Is gulp based on node?