您的位置:首页 > 其它

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

2016-06-24 12:12 309 查看
<!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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: