The substr method returns a substring of the specified length starting at the specified position.
stringvar.substr(start [, length ])
Parameter stringvar
Will be options. The String literal or String object to extract the substring.
start
Will be options. The starting position of the desired substring. The index of the first character in the string is 0.
length
Optional. The number of characters that should be included in the returned substring.
Note If length is 0 or negative, an empty string is returned. If this parameter is not specified, the substring continues to the end of stringvar.
Example The following example demonstrates the use of the substr method.
function SubstrDemo(){ var s, ss; // Declare variables. var s = "The rain in Spain falls mainly in the plain."; ss = s.substr(12, 5); // Get the substring. return(ss); // return "Spain". }Copy the code
The substring method returns a substring at the specified position in the String.
strVariable.substring(start, end)
"String Literal".substring(start, end)
Copy the code
Parameters of the start
Specifies the starting position of the substring, starting at 0.
end
Specifies the end position of the substring, starting at 0.
The substring method returns a string containing substrings from start to the end (without end).
The substring method uses the smaller of both start and end as the starting point for the substring. For example, strvar.substring(0, 3) and strvar.substring(3, 0) will return the same substring.
If start or end is NaN or negative, replace it with 0.
The length of the substring is the absolute value of the difference between start and end. For example, the length of the substring returned in strvar.substring(0, 3) and strvar.substring(3, 0) is 3.
Example The following example demonstrates the use of the substring method.
function SubstringDemo(){ var ss; // Declare variables. var s = "The rain in Spain falls mainly in the plain.." ; ss = s.substring(12, 17); // take a substring. return(ss); // Returns a substring. }Copy the code
Js takes decimal integer part function
Js :parseInt(7/2)
Js: math.ceil (7/2)
Js: math.round (7/2)
Js: math.floor (7/2)
How to determine the numeric type of the input (use without quotes!) It’s just regular expressions. “^\d+$” // non-negative integer (positive integer +0)
“^ [1-9] [0-9] [0-9] $” / / positive integer
“^ ((\ d +) | (0 +)) $” / / not a positive integer (negative integers + 0)
“^ – [1-9] [0-9] [0-9] $” / / negative integer
“^ -? \ d + $” / / integer
“^\d+(\.\d+)? $” // non-negative float (positive float +0)
“^ (([0-9] + \. [1-9] [0-9] [0-9]) | ([0-9] [1-9] [0-9] \. [0-9] +) | ([0-9] [1-9] [0-9])) $” / / is floating point number
“^((-\d+(\.\d+)?) | (0 + (\. 0 +)? )$”// non-positive float (negative float +0)
“^ (- (([0-9] + \. [1-9] [0-9] [0-9]) | ([0-9] [1-9] [0-9] \. [0-9] +) | ([0-9] [1-9] [0-9]))) $” / / negative floating point number
“^ (-? \d+)(\.\d+)? $” / / floating point number
For example,
Var r = /^[0-9]*[1-9][0-9]*$ // STR returns true or false for the character you want to judgeCopy the code