1、字符串合并操作:“ + ”
var iNum01 = 12; var iNum02 = 24; var sNum03 = '12'; var sTr = 'abc'; alert(iNum01+iNum02); //彈出36 alert(iNum01+sNum03); //彈出1212 數(shù)字和字符串相加等同于字符串相加 alert(sNum03+sTr); // 彈出12abc
2、parseInt() 將數(shù)字字符串轉(zhuǎn)化為整數(shù)
var sNum01 = '12'; var sNum02 = '24'; var sNum03 = '12.32'; alert(sNum01+sNum02); //彈出1224 alert(parseInt(sNum01)+parseInt(sNum02)) //彈出36 alert(sNum03) //彈出數(shù)字12 將字符串小數(shù)轉(zhuǎn)化為數(shù)字整數(shù)
3、parseFloat() 將數(shù)字字符串轉(zhuǎn)化為小數(shù)
var sNum03 = '12.32' alert(parseFloat(sNum03)); //彈出 12.32 將字符串小數(shù)轉(zhuǎn)化為數(shù)字小數(shù)
4、split() 把一個(gè)字符串分隔成字符串組成的數(shù)組
var sTr = '2017-4-22'; var aRr = sTr.split("-"); var aRr2= sTr.split(""); alert(aRr); //彈出['2017','4','2'] alert(aRr2); //彈出['2','0','1','7','-','4','-','2','2']
5、charAt() 獲取字符串中的某一個(gè)字符
var sId = "#div1"; var sTr = sId.charAt(0); alert(sTr); //彈出 #
6、indexOf() 查找字符串是否含有某字符
var sTr = "abcdefgh"; var iNum = sTr.indexOf("c"); alert(iNum); //彈出2
7、substring() 截取字符串 用法: substring(start,end)(不包括end)
var sTr = "abcdefghijkl"; var sTr2 = sTr.substring(3,5); var sTr3 = sTr.substring(1); alert(sTr2); //彈出 de alert(sTr3); //彈出 bcdefghijkl
8、toUpperCase() 字符串轉(zhuǎn)大寫
var sTr = "abcdef"; var sTr2 = sTr.toUpperCase(); alert(sTr2); //彈出ABCDEF
9、toLowerCase() 字符串轉(zhuǎn)小寫
var sTr = "ABCDEF"; var sTr2 = sTr.toLowerCase(); alert(sTr2); //彈出abcdef
字符串反轉(zhuǎn)
var str = 'asdfj12jlsdkf098'; var str2 = str.split('').reverse().join(''); alert(str2);