Home >Web Front-end >JS Tutorial >How to get specified characters in javascript
Javascript method to get specified characters: 1. Extract the characters between the two specified subscripts in the string through the substring method; 2. Get the specified number of characters through the substr method; 3. Extract through the slice method part of a string.
The operating environment of this article: windows7 system, javascript version 1.8.5, Dell G3 computer.
How to get specified characters in javascript?
Javascript extracts strings, detailed explanation of JavaScript interception string methods
1. substring()
(1) Used to extract characters The character in the string between the two specified subscripts. Syntax: stringObject.substring(start,stop)
start: required. A non-negative integer that specifies the position of the first character in the stringObject of the substring to be extracted.
Stop; Optional. A nonnegative integer that is one position in the stringObject that is one more than the last character of the substring to be extracted. If this parameter is omitted,
then the returned substring will go to the end of the string.
(2)Return value
A new string value contains a substring of stringObject, whose content is all characters from start to stop-1, Its length
is stop minus start. (The subscript starts from 0) The substring returned by the
substring() method includes the characters at start, but does not include the characters at end. If the parameters start and end are equal, then this method returns an empty string (that is, a string with a length of 0). If start is greater than end, the method swaps the two parameters before extracting the substring.
Important: Unlike the slice() and substr() methods, substring() does not accept negative arguments.
Example 1:
<script type="text/javascript"> var str="Hello world!" document.write(str.substring(3)) //输出 lo world! </script>
Example 2:
<script type="text/javascript"> var str="Hello world!" document.write(str.substring(3,7)) //输出 lo w </script>
Recommended study: "
javascript basic tutorial2 , substr() method(1) The substr() method can extract the specified number of characters starting from the start subscript in the string. stringObject.substr(start,length)
start: required. The starting index of the substring to be extracted. Must be a numeric value. If it is a negative number, then this parameter declares the
position starting 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.
length: 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 stringObject is returned.
(2) Return value
A new string containing lenght characters starting from the start of stringObject (including the character pointed to by start). If lenght is not specified, the returned string contains characters from start to the end of the stringObject.
(3) The parameters of substr() specify the starting position and length of the substring, so it can be used instead of substring() and slice().
(4)Instance 1:4ec11beb6c39d0703d1751d203c17053
var str="Hello world!" document.write(str.substr(3)) //输出 lo world! </script>
Instance 2:4ec11beb6c39d0703d1751d203c17053
var str="Hello world!" document.write(str.substr(3,7)) //输出 lo worl </script>3. slice() method
(1) can extract a certain part of the string and return the extracted part as a new string. stringObject.slice(start,end)start: The starting index of the segment to be extracted. If it is a negative number, this parameter specifies the position starting from the end of the string. That is,
-1 refers to the last character of the string, -2 refers to the second-to-last character, and so on.
end: The index immediately following the end of the segment to be extracted. If this parameter is not specified, the substring to be extracted includes the
character string from start to the end of the original string. If this parameter is negative, it specifies the position from the end of the string.
(2)
Return value
A new string. Includes all characters of the string stringObject from start (inclusive) to end (exclusive).
(3) Example 1:
<script type="text/javascript"> var str="Hello happy world!" document.write(str.slice(6)) //输出:happy world! </script>
Example 2:
<script type="text/javascript"> var str="Hello happy world!" document.write(str.slice(6,11)) //输出: happy </script>
Description
String object’s methods slice(), substring() and substr( ) (deprecated) can return the specified part of the string. slice() is more flexible than substring()
because it allows negative numbers as arguments. slice() differs from substr() in that it specifies a substring in terms of two character positions, whereas
substr() specifies a substring in terms of character position and length.
The above is the detailed content of How to get specified characters in javascript. For more information, please follow other related articles on the PHP Chinese website!