Home > Web Front-end > JS Tutorial > body text

How to get specified characters in javascript

藏色散人
Release: 2023-01-11 09:19:22
Original
14243 people have browsed it

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.

How to get specified characters in javascript

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>
Copy after login

Example 2:

<script type="text/javascript">
var str="Hello world!"
document.write(str.substring(3,7))   //输出 lo w
</script>
Copy after login

Recommended study: "

javascript basic tutorial

"

2 , 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:

Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template