String,math common functions in JS

Source: Internet
Author: User
Tags first string

String object:
1.length Properties
Description: Gets the length of the string
Instance:
var str= "abc";
var i=str.length;//output:3



2.charAt () method
Description: Finds a character in a specified index (position) from a string
Instance:
var str= "abc";
var str1=str.charat (2);//output:c
String index starting from 0



3.indexOf () method
Description: Gets the position (subscript) where the substring first appears in the parent string, and outputs "1" if it is not found;
Format: Index (sub-string, starting index);
Instance:
var str= "abcdb";
var i=str.indexof (' B ');//output:1
var j=str.indexof (' B ', 2);//output:4



4.lastIndexOf () method
Description: Gets the position where the substring first appears in the parent string, unlike IndexOf, where LastIndexOf starts the search from the last character.
Enter "-1" if not found
Instance:
var str= "ABCDCBD";
Alert (Str.lastindexof (' BD '));//output:5
Alert (Str.lastindexof (' B ', 3));//output:1, here the second parameter is 3 for the end of index 3, so the output is 1.
Alert (Str.lastindexof (' B ', 5));//output:5, here the second parameter is 5 for the end of index 5, so the output is 5.
Note: LastIndexOf is a search starting from the last character, then you specify: Str.lastindexof (' B ', 5) It will search for the index of ' B ' forward from the position of index 5



4.substring () method
Description: A partial string that intercepts a female string.
Format: substring (starting index, terminating index)
Instance:
var str= "ABCDEFG";
Alert (str.substring (0,3));//output:abc
Alert (str.substring (3,0));
The output:abc,substring () function automatically determines which is the starting point and that is the termination.
So the parameter 0 is put to the front, the parameter 3 is put in the back, so the output is: ABC
Alert (str.substring (3));//OUTPUT:DEFG

Note: The return value does not contain the character of the sleepover Index, but until the previous character.



5.replace () method
Description: The substring specified in the replacement string is the corresponding string
Format: replace ("Specified substring", "Replace with corresponding string")
Instance:
var str= "ABCDBFGBC";
Alert (str.replace ("BC", ' KK '));//OUTPUT:AKKDBFGBC
Note: Replace only the first string that matches
This is different from C #, see the following example:
String tt = "ABCDCD";
Console.WriteLine (TT. Replace ("CD", "AA"));
Output: Abaaaa,c# 's Replac function searches the string for all "CD" Strings and replaces the "CD" String with "AA".

Math function

1. Max function syntax

Math.max (A, b,..., x, y);

Max function Parameters: A, b,..., x, y--numbers of type number, can be decimals, integers, positive, negative, 0.


2. Max function return value
Returns the larger value in a number of values

3. Math Other related functions:
Math.random ()
Returns a random number between 0 and 1.


4, Math.Round ()
Rounds rounding.


5, Math.ceil ()
Return value: Returns the integer that is greater than or equal to x and closest to.
Note: If x is a positive number, the decimal is "in" and if X is a negative number, the decimal is "placed".


6, Math.floor ()
Return value: Returns the integer that is less than or equal to x and closest to.
Note: If x is a positive number, the decimal is "in" and if X is a negative number, the decimal is "in".

String,math common functions in JS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.