I. Overview of BASIC packaging types
is a basic type, but it is a special reference type.
var box = ' Mr.Lee '; Basic type
Alert (box.substring (2)); Object. Method (parameter), which is clearly the notation of a reference type
Substring index starts at 0 and intercepts the string at the end from the 2nd position
Alert (' Mr.Lee '. SUBSTRING (2)); This kind of writing is also feasible.
Literal notation
var box = ' Mr.Lee '; Basic type
Box.name = ' Lee '; Adding attributes to a base type
Box.age = function ()//Add method to base type
{
return 100;
};
alert (box);
Alert (typeof box);
alert (box.name); Undefined
Alert (Box.age ()); Error
//Basic types are not able to create properties and methods for themselves.
New operator notation
var box = new String (' Mr.Lee '); Reference type of string
Box.name = ' Lee ';
Box.age = function ()
{
return 100;
};
alert (box);
Alert (typeof box); Object
alert (box.name);
Alert (Box.age ());
var box = 100;
Alert (box. Max_value); This notation is called an attribute.
alert (Number.MAX_VALUE); This notation (type. property), called a static property
var box = 1000.789;
Alert (typeof box.tostring ()); Converts a value to a string, and can be converted into a binary
Alert (box.tolocalestring ()); Convert to a string based on local numeric formatting
Alert (box.tofixed (2))//decimal point reserved Two bits, and convert string.
Alert (box.toexponential ()); In exponential form, and convert strings.
Alert (Box.toprecision (8)); Determine the exponent or number of points according to the parameters
Four. String type
The string type contains three properties and a large number of available built-in methods
var box = ' Mr.Lee ';
alert (box.length); Returns the character length of a string
alert (box.constructor);//returns the function that created the string object
alert (Box.prototype); Extending a string definition by adding properties and methods
Character method
var box = ' Mr Lee ';
Alert (Box.charat (1)); Returns the specified value of the underlying
Alert (Box.charcodeat (4));//Return ACSSII code
String manipulation methods
var box = ' Mr Lee ';
Alert (Box.concat (' is ', ' Teacher ', '! ')); String connection
Alert (Box.slice (4,6)); String that returns the position of the string N to M
Alert (box.substring (4,6)); String that returns the position of the string N to M
Alert (BOX.SUBSTR (4,6)); Returns the first m string of string n
Alert (Box.slice (-2)); 7+ (-2) = 5, beginning of 5th position, EE.
Alert (Box.substring (-2)); Negative numbers return all strings
Alert (Box.substr (-2)); 7+ (-2) = 5, beginning of 5th position, EE.
Alert (Box.slice (2,-1)); 7+ (-1) = 6, (2,6);
Alert (Box.slice ( -2,-1)); 7+ (-2) = 5, 7+ (-1) = 6, (5,6)
Alert (box.substring (2,-1)); If the parameter is negative, return 0 directly, (2,0) if the second parameter is smaller than the first one, then it becomes (0,2);
Alert (BOX.SUBSTR (2,-1)); The second parameter is a negative number and returns 0 directly. (2.0) Choose 0 from the second start, which is the null value.
String Position method
var box = ' Mr lee is Lee ';
Alert (Box.indexof (' L ')); Returns the position of the first occurrence of the search from the initial position, 4.
Alert (Box.lastindexof (' L ')); Returns the position of the first occurrence of the search from the end position, 11.
Alert (Box.indexof (' L ', 5)); Starting from the 5th position search L First occurrence position, 11.
Alert (Box.lastindexof (' L ', 5)); Starting from the 5th position, search forward the first occurrence of the position, 4.
Alert (Box.indexof (', ')); Returns 1 if not found
Example: Find out all the L
var boxarr = []; Array to store L position
var pos = box.indexof (' L '); Get the first L position first, put in the variable pos
while (Pos >-1)
{
Boxarr.push (POS); Add to Array
pos = Box.indexof (' L ', pos+1); Re-assign POS current location
}
alert (Boxarr);
Uppercase and lowercase conversion methods
var box = ' Mr Lee ';
Alert (Box.tolowercase ()); Convert all to lowercase
Alert (Box.touppercase ()); Convert all to Uppercase
Alert (Box.tolocalelowercase ()); Convert all to lowercase and localized
Alert (Box.tolocaleuppercase ()); Convert all to uppercase and localized
Pattern matching method for strings
var box = ' Mr Lee ';
Alert (Box.match (' L ')); The string is returned when the string is found
Alert (Box.match (', ')); Return NULL if not found
Alert (Box.search (' L ')); Find the location of L.
Alert (box.replace (' L ', ' Q ')); Replace the found string
Alert (Box.split (")); Split into strings with spaces
Other methods
Alert (String.fromCharCode (76)); Find the letter corresponding to the ASCCII code
var box = ' Lee ';
Alert (Box.localecompare (' Lee ')); 1
Alert (Box.localecompare (' Aee ')); 0
Alert (Box.localecompare (' Zee ')); -1
Alert (Box.localecompare (' 6576 ')); 1
Alert (box.localecompare (' yes ')); -1
HTML method
var box = ' Lee ';
Alert (Box.link (' http://www.baidu.com ')); Link Add Hyperlink method
Alert (Box.bold ()); Bold method
JS Basic Package Type