javascript数据类型示例分享_基础知识
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript数据类型示例分享_基础知识,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3844字,纯文字阅读大概需要6分钟。
内容图文
![javascript数据类型示例分享_基础知识](/upload/InfoBanner/zyjiaocheng/324/b6cf2ad66b524108b675f1c38be540e6.jpg)
输出结果中小数的位数
var num = 10;
alert(num.toExponential(1));//"1.0e+1"
//不过这么小的数字就不需要使用指数表示法了,如果你想得到某个数值最合适的格式就应该使用
//toPrecision()方法,此方法可能返回固定大小(fixed)格式,也可能返回指数(exponential)格式
//接受一个参数表示数值所有数字的位数(不包括指数部分)。
var num = 99;
alert(num.toPrecision(1));//1e+2,1e+2表示100,因为指数无法表示99所以向上舍入变成100
alert(num.toPrecision(2));//"99"
alert(num.toPrecision(3));//"99.0"
//String对象,String对象的方法也可以在所有的基本字符串中访问到。
//1、字符操作方法:charAt()、charCodeAt()。每个参数都接受一个基于位置0的字符位置
var stringValue = "Hello world!";
stringValue.charAt(1);//"e" 第二个位置是“e”
stringValue.charCodeAt(1);//"101" 第二个位置“e”的字符编码是“101”
//2、字符串操作方法concat(拼接的字符)、slice(index,index)、substring(index,index)、substr(index,length)。index:位置,length:长度
var str1 = "hello";
alert(str1.concat(" word"));//Hello world
alert(str1.concat(" word", "!"));//Hello world!
var stringValue = "Hello world!";
alert(stringValue.slice(3));//lo world
alert(stringValue.substring(3));//lo world
alert(stringValue.substr(3));//lo world
alert(stringValue.slice(3, 7));//lo w
alert(stringValue.substring(3, 7));//lo w
alert(stringValue.substr(3, 7));//lo worl 这个7代表截取的长度
//3、字符串位置方法 indexOf() 和 lastIndexOf()
//这两个方法都是从指定的字符串中搜索给定的字符串,然后返回字符串的位置,没有找到就返回-1。
//这两个方法的区别在于一个是从字符串的开头向后搜索字符串,而lastIndexOf是从字符串的末尾向前搜索字符串。
//这两个方法都有一个可选的参数(从指定的位置开始搜索)
var stringValue = "hello word";
alert(stringValue.indexOf("o"));//4
alert(stringValue.lastIndexOf("o"));//7
//可以循环调用indexOf或lastIndexOf来找到指定的字符串
var stringValue = "wo de wei lai bu shi meng!wo men you geng hao de ming tian!";
var positions = [];
var pos = stringValue.indexOf("e");
while (pos > -1) {
positions.push(pos);
pos = stringValue.indexOf("e", pos + 1);
}
alert(positions);//4、7、22、33、38、47
//4、trim()这个方法会创建一个字符串副本,删除前置及后置的所有空格。
var stringValue=" hello word ";
alert(stringValue);
alert(stringValue.trim());
//5、字符串大小写转换方法
//toLowerCase、toLocalLowerCase、toUpperCase、toLocalUpperCase
var stringValue="hello word";
alert(stringValue.toLocaleUpperCase());//此方法比较稳妥
alert(stringValue.toUpperCase());
alert(stringValue.toLocaleLowerCase());//此方法比较稳妥
alert(stringValue.toLowerCase());
//6、字符串匹配方法 replace()
//这个方法接受两个参数,第一个参数是一个正则表达式或者字符串,第二个参数是一个字符串或一个函数
var text="cat,bat,sat,fat";
var result=text.replace("at","ond");//
alert(result);//"cond,bond,sond,fond"
var result=text.replace(/at/g,"ond");//
alert(result);//"cond,bond,sond,fond"
var text="cat,bat,sat,fat";
result=text.replace(/(.at)/g,"word ($1)");
alert(result);
//replace的第二个参数也可以是一个函数
function htmlEscape(text) {
//函数有是三个参数:1、模式匹配项 2、模式匹配项在字符中的位置 3、原始字符串
return text.replace(/[<>"&]/g,function(match,index,text){
switch (match){
case "<":
return "<";
case ">":
return ">";
case "&":
return "&";
case "\"":
return """;
}
});
}
alert(htmlEscape("
Hello World!
"));//
Hello World!
//localCompare()比较两个字符串。A.localCompare("B")
//如果字符串(A)在字母表中排在字符串参数(B)之前,这返回负数(-1)
//如果字符串等于字符串参数则返回0
//如果字符串(A)在字母表中排在字符串参数(B)之后则返回正数(1)
var stringValue="f";
alert(stringValue.localeCompare("d"));//1
alert(stringValue.localeCompare("f"));//0
alert(stringValue.localeCompare("z"));//-1
//fromCharCode 这个静态方法是与charCodeAt执行相反的操作
alert(String.fromCharCode(104,101,108,108,111));//"hello"
//7、html方法建议不要使用。
</ script>
END
童鞋们是否对javascript的数据类型有了新的认识了呢,希望大家能够喜欢。
内容总结
以上是互联网集市为您收集整理的javascript数据类型示例分享_基础知识全部内容,希望文章能够帮你解决javascript数据类型示例分享_基础知识所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。