Basic Package Type
<Script> // Basic Package Type var STR = 'Andy' ; the console.log (str.length); // objects have properties and methods of complex data types have attributes and methods // simple data types why length property it? // Basic Package Type: data type is the simple packaging has become a complex data type // (1) the simple data type is a complex data type packaging var TEMP = new new String ( 'Andy' ); // (2) the temporary variable the value to the STR STR = TEMP; // (. 3) destruction of the temporary variable TEMP = null ; </ Script>
Immutability of strings
<Script> // immutability string var STR = 'Andy' ; the console.log (STR); STR = 'Red' ; the console.log (STR); // because our string immutable so do a lot of string concatenation var STR = '' ; for ( var I =. 1; I <= 1000000000; I ++ ) { STR + = I; } the console.log (STR); </ Script>
Returns the position of the character
<Script> // String object to return the character position str.indexOf ( 'the character to find', [the starting position]) var STR = 'reform the revival of the floor, to the spring' ; the console.log (STR .indexOf ( 'spring' )); the console.log (str.indexOf ( 'spring', 3)); // back to find the start position of the index number 3 is </ script>
Find the location and the number of strings "abcoefoxyozzopp" all appear o
<Script> // search string position "abcoefoxyozzopp" all appear and the number o // core algorithms: the first find the location of the first occurrence of a o // then return results as long as indexOf is not -1 would continue in the future to find / / because indexOf only to find the first, so the back of the lookup, the current index must be incremented by one, thereby continuing to find var STR = "oabcoefoxyozzopp" ; var index = str.indexOf ( 'O' ); var NUM = 0 ; // the console.log (index); the while (index == -1! ) { the console.log (index); NUM ++ ; index = str.indexOf ( 'O', + index. 1 ); } the console.log ('it is the number of occurrence o:' + NUM); // homework [ 'red', 'blue' , 'red', 'green', 'pink', 'red'], and the number of required positions appear red </ script>
Returns the character (Key) based on the location
<Script> // The position of the character to return // 1. the charAt (index) Returns the character based on the position var STR = 'Andy' ; the console.log (str.charAt ( . 3 )); // traverse all characters for ( var I 0 =; I <str.length; I ++ ) { the console.log (str.charAt (I)); } // 2. the charCodeAt (index) returns the ASCII value of the character object corresponding to the index number: determining the key pressed by the user the console.log (str.charCodeAt (0)); // 97 // 3. STR [index] the H5 added the console.log (STR [0]); // a </ Script>
Determining the largest number of characters of a string 'abcoefoxyozzopp' appears in and count the number thereof.
<Script> // there is an object to determine whether the object has the attribute [ 'attribute name'] var O = { Age: 18 is } IF (O [ 'Sex' ]) { the console.log ( 'which has the property' ) ; } the else { the console.log ( 'the attribute is not' ); } // C // OA =. 1 // OB. 1 = // OC =. 1 // OO =. 4 // core algorithms: using the charAt () iterates through the string // each character is stored to the object, if the object does not have this property, it is one, if it exists +1 // traverse the object, to obtain the maximum value and the character var= STR 'abcoefoxyozzopp' ; var O = {}; for ( var I = 0; I <str.length; I ++ ) { var chars = str.charAt (I); // chars every character of the string IF ( O [chars]) { // O [chars] is obtained attribute value O [chars] ++ ; } the else { O [chars] =. 1 ; } } the console.log (O); // 2. traverse the object var 0 = max ; var CH = '' ; for ( var K in O) { // K is obtained property name // O [K] obtained is the attribute value IF (O [K]> max) { max = O [K]; CH = K; } } Console .log (max); the console.log ( 'character is the most' + CH); </ Script>
<Script> // String Operation // 1. the concat ( 'string 1', 'string 2' ....) var STR = 'Andy' ; the console.log (str.concat ( 'Red' ) ); // 2. substr ( 'starting position taken' ', taken several characters'); var str1 = 'reform the revival floor' ; the console.log (str1.substr ( 2, 2)); / / second from a 2 2 2 several start of the second is to take the number of characters of the index </ script>
Alternatively string segmentation and
<Script> // 1. Replace replacement character ( 'character is replaced by a', 'character is replaced') only replaces the first character var STR = 'andyandy' ; the console.log (str.replace ( 'A ',' B ' )); // a string' abcoefoxyozzopp 'which requires to replace all o * var str1 =' abcoefoxyozzopp ' ; the while (str1.indexOf ('! o ') == -1 ) { str1 = str1.replace ( 'O', '*' ); } the console.log (str1); // 2. Split into an array of characters ( 'separator') join the front we have learned the array to a string var = str2 'Red, Pink, Blue' ; Console.log(str2.split(',')); var str3 = 'red&pink&blue'; console.log(str3.split('&')); </script>