JavaScript&jQuery.String对象.Number对象.Math对象

String对象.Number对象.Math对象


String对象

String 对象的属性和方法用于操作字符串。

<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
var str="I like javascript ";
document.write(str);
document.write('<br>');
// 属性长度
document.write(str.length);
document.write('<br>');
// 转大写
document.write(str.toUpperCase());
document.write('<br>');
// 转小写
document.write(str.toLowerCase());
document.write('<br>');
// 返回指定位置的字符,不包括空
document.write(str.charAt(5));
document.write('<br>');
// 返回字符的位置
document.write(str.indexOf('a'));
document.write('<br>');
// 返回字符最后一次出现的位置
document.write(str.lastIndexOf('a'));
document.write('<br>');
// 从字符串中取指定范围的字符,从开始,包括空格
document.write(str.substring(0,4));
document.write('<br>');
// 将字符串按分解规则分解成数组
var value=str.split(" ");
document.write(value[0]);
document.write('<br>');
// 去年字符串开始和结尾的空格
document.write(str.trim());
document.write('<br>');
// 查看和替换
document.write(str.replace('javascript','C++'));
document.write('<br>');
</script>
</body>
</html>

Math对象

Number对象通常用于操作数字。

<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div id="info"></div>
<script>
var numberOne=12.545678;
// 四舍五入
document.write(numberOne.toFixed());
document.write('<br>');
// 指定有效数个数,这里指定为3
document.write(numberOne.toPrecision(3));
document.write('<br>');
//以指数表示法返回该数值字符串表示形式,可以带参数指定小数位数
document.write(numberOne.toExponential(3));
document.write('<br>');
</script>
</body>
</html>

Math对象
Math对象用于数学上的计算,如算平方,四舍五入,生成随机数等等。
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    var numberTwo=23.344;
    // 常量PI
    document.write(Math.PI);
    document.write('<br>');
    // 生成随机数,0~1,范围从0到1以内,不包括1
    document.write(Math.random());
    document.write('<br>');
    // 将整数,取最近并大于它当前值的数
    document.write(Math.ceil(numberTwo));
    document.write('<br>');
    // 将整数,取最近并小于它当前值的数
    document.write(Math.floor(numberTwo));
    document.write('<br>');
    // 四舍五入
    document.write(Math.round(numberTwo));
    document.write('<br>');
</script>
</body>
</html>

 

猜你喜欢

转载自www.cnblogs.com/H97042/p/9159856.html