基本包装类型Number类型、String类型、Boolean类型的方法

基本包装类型String

属性:3种
var box='Mr. Lee';
alert(box.length); //7

var box='Mr. Lee';
alert(box.constructor); //返回创建String对象的函数 function String() { [native code] }

prototype:通过添加属性和方法扩展字符串定义
------------------------------------------------------
通用方法:
valueOf()、toLocaleString()和toString()方法,但这些方法都返回字符串的基本值
------------------------------------------------------
两个字符方法:
var box='Mr. Lee';
alert(box.charAt(1)); //r 打印出指定下标的值
alert(box.charCodeAt(1)); //L,76 返回的是acssii码
------------------------------------------------------
字符串操作方法
var box='Mr. Lee';
alert(box.concat(' is ','Teacher','!'));

var box='Mr. Lee';
alert(box.slice(4,6)); //Le 返回字符串n到m之间位置的字符串
alert(box.substring(4,6)); //Le 返回字符串n到m之间位置的字符串
alert(box.substr(4,2)); // 返回字符串4开始的2个字符串

alert(box.slice(4));
alert(box.substring(4));
alert(box.substr(4)); //三个都是Lee

负数:
var box='Mr. Lee';
alert(box.slice(-2)); //ee 字符串总长度7+(-2)=5 ,第五位开始
alert(box.substring(-2)); //负数返回全部字符串 Mr. Lee
alert(box.substr(-2)); //ee 字符串总长度7+(-2)=5 ,第五位开始 IE浏览器遇到负值会全部返回

两个参数:
var box='Mr. Lee';
alert(box.slice(2,-1)); //7+(-1)=6,(2,6), . Le
alert(box.slice(-2,-1)); //e 7+(-2)=5,7+(-1)=6, (5,6)
alert(box.substring(2,-1)); //Mr 参数如果是负,直接转换为0,(2,0),如果第二个参数比第一个小,那么第二个参
数提前(0,2)
alert(box.substr(3,-1)); //第二个参数为负,直接转0,(3,0)


substring
var box='Mr. Lee'; //基本类型
alert(box.substring(2)); // . Lee 对象.方法(参数),这种写法明显是引用类型的写法
//索引从0开始,从第2个位置开始截取到末尾的字符串输出
//是基本类型,但又是特殊的引用类型,又成为基本包装类型
//因为他可以调用系统内置的方法
alert('Mr. Lee'.substring(2)); //有效

------------------------------------------------------
字符串位置方法:
var box='Mr. Lee is Lee';
alert(box.indexOf('L')); //返回从初始位置搜索L第一次出现的位置,4
alert(box.lastIndexOf('L'));//返回从末尾位置搜索L第一次出现的位置,11
alert(box.indexOf('L',5)); //从第5个位置开始搜索L第一次出现的位置
alert(box.lastIndexOf('L',5)); //从第5个位置开始向前搜索L第一次出现的位置,4
alert(box.indexOf(',')); //找不到,返回-1

var box='Mr. Lee is Lee';
var boxarr=[];
var pos=box.indexOf('L');
while(pos>-1){
boxarr.push(pos);
pos=box.indexOf('L',pos+1)
}
alert(boxarr);
------------------------------------------------------
大小写转换
var box='Mr. Lee is Lee';
alert(box.toLowerCase()); //全部小写
alert(box.toUpperCase()); //全部大写
alert(box.toLocaleLowerCase());
alert(box.toLocaleUpperCase()); //PS:只有几种语言(如土耳其语)具有地方特有的大小写本地性,一般来说,是否
本地化效果都是一致的。
------------------------------------------------------
match()、replace()、search()、split()在普通字符串中也可以使用
var box='Mr. LeeL';
alert(box.match('L')); //找到L即返回L
alert(box.match(',')); //没有找到返回null
alert(box.search('L')); //找到L的位置 4
alert(box.replace('L','Q')); //Mr. QeeL 替换
var box='Mr. LeeL';
alert(box.split(' ')); //Mr.,LeeL

----------------------------
alert(String.fromCharCode(76)); //里面放一个ascii码

var box='Lee';
alert(box.localeCompare('Lee')); //0
alert(box.localeCompare('Aee')); //1
alert(box.localeCompare('Zee')); //-1

------------------------------------------------------
HTML方法:
var box='百度';
alert(box.link("http://www.baidu.com")); //<a href="http://www.baidu.com">百度</a>
alert(box.bold()); //<b>百度</b>
------------------------------------------------------------------------------------------------------
基本包装类型Boolean

Boolean 类型没有特定的属性或方法。

------------------------------------------------------------------------------------------------------
基本包装类型Number
静态属性:
alert(Number.MAX_VALUE); //类型.属性,静态属性
MIN_VALUE
NaN
NEGATIVE_INFINITY 负无穷大
POSITIVE_INFINITY 无穷大
prototype 原型,用于增加新属性和方法


var box=1000;
alert(box.toString()); //1000
alert(typeof box.toString()); //string

var box=1000;
alert(box.toLocaleString()); //1,000

var box=1000.789;
alert(box.toFixed(2)); //1000.79 小数点保留两位,并转换字符串 四舍五入
alert(typeof box.toFixed(2)); //string

var box=1000.789;
alert(box.toExponential()); //以指数形式 1.000789e+3 并转换字符串
alert(typeof box.toExponential()); //string

var box=1000.789;
alert(box.toPrecision()); //1000.789
alert(box.toPrecision(2)); //1.0e+3
alert(box.toPrecision(8)); //1000.7890 根据传参来决定指数或者点数

posted @ 2017-07-14 16:52  耿鑫  阅读(267)  评论(0编辑  收藏  举报