JavaScript中的数据类型转换
2011-08-16 18:44
JavaScript中的数据类型转换
一、自动类型转换 1、documet.write() 格式:document.write("You are the best!") 用于在当前网页输出相应的内容。 2、负负得正 例:var e="5"; document.write(-(-e)); 自动将字符串转换成了数字类型。 3、运算转换 例:var e="5"; document.write(e-1+1); 二、显式类型转换 1、toString() 格式:toString(要转换的数据内容) Global对象的toString()方法用于将一种类型的数据转换成为字符串类型。 2、toUpperCase()、toLowerCase() 格式:字符串对象.toUpperCase() 字符串对象.toLowerCase() 他们是 String 对象的方法,toUpperCase() 将字符串转变为大写,toLowerCase()将字符串转变为小写。 3、parseInt()、parseFloat() 格式:parseInt(字符串) parseFloat(字符串) 他们是Global 对象的方法,用于将字符串转换为相应的数字类型。 4、typeof() 格式:typeof(toString(true)) 他是用于返回一个字符串,指明其操作数的数据类型。 三、提升基本类型为对象 String对象的length属性,link()、big()、bold()、fontcolor()、indexOf()、lastIndexOf()、charAt()方法。及使用join()方法将数组转变为字符串。 1、length 格式:String对象.length 返回字符串中包含字符的个数。 2、link() 格式:String对象.link("要链接的目标") 在String对象上添加超级链接标签。 3、big() 格式:String对象.big() 在String对象上添加<big></big>标签。 4、fontcolor() 格式:String对象.fongcolor("颜色值") 为String对象设置相应的颜色。 5、indexOf() 格式:String对象.indexOf("要查找的字符串") 查找字符串在String中首次出现的位置,并返回这个位置编号。若无则返回-1。 6、lastIndexOf() 格式:String对象.lastIndexOf("要查找的字符串") 查找字符串在String中最后出现的位置,并返回这个位置编号。若元则返回 -1。 7、charAt() 格式:String对象.charAt(表示位置的数字) 返回字符串中指定位置的字符。字符串中的字符计数是从0开始的。 8、join() 格式:Array对象.join("要使用的分隔符") 将一数组转换成为一个字符串,使用分隔符将其连接起来。
posted on 2012-11-14 15:40 wokao100000 阅读(106) 评论(0) 编辑 收藏 举报