Basic data type methods

For ease of operation, JS provides three types of reference specific

String /Number/Boolean

1.Number type:

() Num.toString () is converted into a digital string

var num=1246;
console.log( num.toString())    //1246

(2) num.toFixed (n) n significant digits reserved

var num=3.1415926;
console.log( num.toFixed(2))    //3.14

 2.String type

(1) str.indexOf () Find index

 was str = ' ABCD ' 
 was res = str.indexOf ( ' c ' ) 
 console.log (res)   // 2

If you find missing, it returns -1

was str = ' ABCD ' 
 was res = str.indexOf ( ' e ' ) 
 console.log (res)   // -1

Blank (2) str.trim () ends removed

var str='  a b c d  '
 var res=str.trim()
 console.log(res)  //a b c d


(3) str.toUpperCase () conversion of capital

var str='ABcDEf'
 var res=str.toUpperCase()
 console.log(res)  //ABCDEF

    str.toLowerCase () Conversion lowercase

 var str='ABcDEf'
 var res=str.toLowerCase()
 console.log(res)  //abcdef

(4) str1.concat (str2) string concatenation

was str1 = ' abc ' 
was str2 = ' 123 ' 
was str3 = str1.concat (str2) 
console.log (str3)   // abc123

(5) str.slice (start, end) comprises interception start, not including the end

var str='abcdefg'
console.log( str.slice(2,4))  //cd

       str.substr (start, count) interception

var str='abcdefg'
console.log(str.substr(2,2))   //cd

(6) str.split () is converted into an array of strings, and join () method of a string array into the opposite

var str='a-b-c-d-e-g'
console.log( str.split('-'))

 (7) str.replace (/ is replaced by the string / g, 'new string')   

var str='abcadag'
console.log( str.replace(/a/g,'z'))   //zbczdzg

 

var str='a-b-c'
console.log( str.replace(/-/g,''))   //abc

 

var str='a b c d'
console.log( str.replace(/ /g,''))   //abcd

(8) traversing the string

var str='abcdefg'
for(var i=0;i<str.length;i++){
 console.log(str[i]) 
}

(9) Gets the element

var str='abcdefg'
console.log(str[3])   //d

 

Guess you like

Origin www.cnblogs.com/zhaodz/p/11610629.html