indexOf charAt split substring substr字符串和Math常用方法总结

<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>string</title>
</head>
<body>
	<script>
		var str = 'I love JavaScript';
		//输出指定的字母,空格也算一个字符。
		console.log(str.charAt(2))
		//检索某个字符串首次出现的位置,如果不存在返回-1;
		console.log(str.indexOf('o'));
		console.log(str.indexOf('x'));
		//第一个参数是必须的,第二个是非必须的,数字代表检索的位置
		console.log(str.indexOf('m',5))
		//切割字符串,分割为字符串数组
		//字母a当做界限。分割为数组
		console.log(str.split('a'));
		//alert(str.split('a'))
		//第二个参数是可选的,显示的是分隔的次数,也就是显示的元素个数
		console.log(str.split('a',2))
		//提取指定下标的字符串,参数必须是非负整数
		console.log(str.substring(6))
		//第二个参数可选,就是取之间的字符串
		console.log(str.substring(6,8))
		//参数可以是负数,也可以是正数,负数的话是从后往前截取的字符个数
		console.log(str.substr(-3))
		//第二个参数是正数是截取字符串长度
		console.log(str.substr(-3,2))
		console.log(str.substr(7,4))
	</script>
</body>
</html>


<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Math</title>
</head>
<body>
	<script>
		//Math可以直接使用
		//Math.ceil直接向上取整,就是生成的正数是比它大的正数
		console.log(Math.ceil(0.8))
		console.log(Math.ceil(-0.4))
		//向下取整
		console.log(Math.floor(0.8))
		console.log(Math.floor(-0.8))
		//round四舍五入
		console.log(Math.round(0.4));
		console.log(Math.round(-0.6))
		//大于等于0但小于1
		console.log(Math.random())
	</script>
</body>
</html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值