1. 程式人生 > >js string和number

js string和number

efi 圖片 計數 ace visit ont 判斷 his sub

number

Js只有一種數字類型(包括整型,浮點型)

極大或極小的可用科學計數法來表示。(7.7123e+1)

所有js數字均為64位

Js所有的數字都存儲為浮點型

小數的最大位數是17位

0開頭的為八進制 0x開頭的為16進制

console.log(Number.MAX_VALUE); 最大值

console.log(Number.MIN_VALUE);最小值

console.log(Number.NEGATIVE_INFINITY);極大值

console.log(Number.POSITIVE_INFINITY);極小值

IsNaN 判斷是不是NaN

console.log(Number.isNaN(NaN)); true

console.log(Number.isNaN(Number.NaN)); true

console.log(Number.isNaN(0/0)); true

console.log(Number.isNaN(‘NaN‘)); false

console.log(Number.isNaN(‘‘)); false

console.log(Number.isNaN(‘123‘)); false

console.log(Number.isNaN(true)); false

console.log(Number.isNaN(undefined)); false

console.log(Number.isNaN(‘ ‘)); false

toFixed();四舍五入為指定小數位數的數字

var n=12345.6789;

console.log(n.toFixed()); 12346

console.log(n.toFixed(1)); 12345.7

console.log(n.toFixed(2)); 12345.68

console.log(n.toFixed(6)); 12345.678900

console.log(1.23e+20.toFixed(2)); 123000000000000000000.00

console.log(1.23e-20.toFixed(2)); 0.00

console.log(2.45.toFixed(1)); 2.5

toExponential(x);把對象的值轉變為指數計數法,x規定小數位數

var n=77.1234;

console.log(n.toExponential()); 7.71234e+1

console.log(n.toExponential(2)); 7.71e+1

console.log(n.toExponential(4)); 7.7123e+1

Toprecision();對象的值超出指定位數時將其轉換為指數計數法;

var n=5.1234567;

console.log(n.toPrecision()); 5.1234567

console.log(n.toPrecision(1)); 5

console.log(n.toPrecision(2)); 5.1

console.log(n.toPrecision(5)); 5.1235

console.log((1234.567).toPrecision(2)); 1.2e+3

String對象

var str=‘king‘;

console.log(typeof str); //string

var strObj=new String(‘king‘);

console.log(typeof strObj); //obj

console.log(strObj[0]); //k

console.log(strObj.length); //4

console.log(strObj.valueOf()); //king

console.log(strObj.toString()); //king

console.log("nana"[0]); //n

console.log("helloworld".length); //10

//charAt()根據下標返回指定的字符

var str=‘king‘;

console.log(str.charAt(0)); //k

console.log(str.charAt(1)); //i

console.log(str.charAt(2)); //n

console.log(str.charAt(3)); //g

console.log(str.charAt(10)); //

console.log(str.charAt(b)); //-1

//charCodeAt():返回指定字符的ASCII碼值

var str=‘abcdef‘;

console.log(str.charCodeAt(0));//a的ASCII碼值97

console.log(str.charCodeAt(100)); 空的值為 NaN

console.log(str.charCodeAt(-123)); NaN

//fromCharCode():根據指定的ASCII放回對應的字符

console.log(String.fromCharCode(97)); //a

console.log(String.fromCharCode(65,66,67));

var str=‘hello ‘;

console.log(str.concat(‘world‘));

// hello world

console.log(str.concat(‘world ‘,‘!‘));

// hello world!

//字符串搜索相關

var str=‘this is a test‘;

var str=‘this is a test‘;

console.log(str.indexOf(‘t‘)); 0

console.log(str.indexOf(‘is‘)); 2

console.log(str.indexOf(‘Is‘)); -1

console.log(str.indexOf(‘i‘)); 2

console.log(str.indexOf(‘i‘,3)); 5//此處的3指的是從第三位開始

通過indexOf()可以統計一個字符在指定字符串中出現的次數

var str=‘sssssdlkfjlwk34jlksdfjlksjdlf234‘;

var count=0;

var pos=str.indexOf(‘s‘);

while(pos!=-1){

count++;

pos=str.indexOf(‘s‘,pos+1);

}

console.log(count);

lastIndexOf():最後一次出現的位置

var str=‘this is a test‘;
console.log(str.indexOf(‘is‘)); 2
console.log(str.lastIndexOf(‘is‘)); 5

比較兩個字符串

console.log(‘h‘.localeCompare(‘j‘)); 104和106

大 顯示負數 後面小 顯示正數

match():找到一個或多個正則表達式的結果

var str=‘this is a test of king show time‘;
var re=/IS/i; i不區分大小寫

console.log(str.match(re));

var str=‘QWERTYUIOPASDFGHJKLZXCVBNMqwertyuioasdfghjkzxcvbnm‘;
console.log(str.match(/[a-f]/ig)); g是全局匹配(搜索)

search():根據正則表達式進行搜索

var str=‘this is a test‘;
console.log(str.search(/is/));   2
console.log(str.search(/IS/)); -1
console.log(str.search(/IS/i)); 2

var str=‘this is a test‘;
var newStr=str.replace(/IS/ig,‘!‘); th! ! a test 把is替換成!

var str="2015-09-26";
var newStr=str.replace(/(\d{4})-(\d{2})-(\d{2})/,"$2/$3/$1"); 09/26/2015 換位置

截取字符串
var str=‘abcdef‘;
console.log(str.slice(2)); cdef
console.log(str.slice(0,2)); 左開右閉 ab 只顯示0 1 後面閉合相當於沒有

console.log(str.slice(-3)); def
console.log(str.slice(-4,-2)); cd
console.log(str.slice(0,-1)); abcde

console.log(str.substr(3)); def
console.log(str.substr(0,4)); abcd  

split()將字符串拆分成數組
var str=‘2015-08-12‘;
var arr=str.split(‘-‘); ["2015", "08", "12"]
console.log(arr);
var str=‘a b c d e f‘;
var arr=str.split(‘ ‘); ["a", "b", "c", "d", "e", "f"]
console.log(arr);
arr=str.split(‘ ‘,2); ["a", "b"]
console.log(arr);

字符串大小寫相關

(大寫換小寫 小寫換大寫)

console.log("KING".toLowerCase());
console.log("KING".toLocaleLowerCase());
console.log(‘nana‘.toUpperCase());
console.log(‘nana‘.toLocaleUpperCase());

trim()

var str=‘ abc ‘;
alert("!"+str+"!"); 有空格
alert("!"+str.trim()+"!"); 沒有空格

產生錨點
var str="this is a test";
document.body.innerHTML=str.anchor(‘contents_anchor‘); this is a test 把變量this is a test輸入進str.anchor(‘contents_anchor‘); 括號裏面

產生鏈接

var title=‘this is of king show time‘;
var url=‘http://phpfamily.org‘;
document.write(‘Click Me to Visit My Blog‘+title.link(url)); 把鏈接代替到英文字母裏 點字母跳入到鏈接裏

技術分享圖片

js string和number