JS indexOf(),split(),substring(),substr(),Math.ceil(),Math.floor(),Math.round()的内容

indexOf():

var mystr="Hello WorHld!"

括号内若只有一个参数,代表要查找的字符;例:document.write(mystr.indexOf("H"));

括号内若有两个参数,前一个代表要查找的字符,后一个代表从哪个位置开始查找;例:document.write(mystr.indexOf("H",7));

若要查找的字符没有,则返回-1

split():切割符

var mystr="86-010-85468578";

括号内若填 "-" ,则表示在"-"切割,结果为:86,010,85468578

括号内若填 "-" ,2 ,则表示在"-"切割,切两段,结果为:86,010

括号内若填 "" ,则每个字符都会被分割,结果为:8,6,-,0,1,0,-,8,5,4,6,8,5,7,8

括号内若填 "",3 ,则前三个字符都会被分割,结果为:8,6,-

substring():提取字符串

var mystr="Hello World!"

当substring()的括号内填 一个(数字)参数 a,则表示返回从a(包括a)开始一直到  结尾.length-1  的字符

当substring()的括号内填 两个(数字)参数  a,b ,则表示返回从a(包括a)开始一直到b的字符

substr():提取指定数目

var mystr="Hello World!";

当括号内填一个(数字)参数a时,表示从a(不包括a)开始一直到  结尾

当括号内填两个(数字)参数 a,b 时,表示从a开始,到后面的 b 个字符,例:document.write(mystr.substr(0,5));——输出:Hello

var mystr="Hello World!";

-1是倒数第一个字符(!),-2是倒数第二个字符(d!),依此类推

Math.ceil():向上取整数

例:document.write(Math.ceil(-9.9));——输出:-9

document.write(Math.ceil(-0.1));——输出:0

Math.floor():想下取整

例:document.write(Math.floor(-9.9));——输出:-10

document.write(Math.floor(-0.1));——输出:-1

Math.round():四舍五入

例:document.write(Math.round(-9.9));——输出:-10

document.write(Math.round(-0.1));——输出:0

猜你喜欢

转载自www.cnblogs.com/chenyuan7/p/9164946.html
今日推荐