javascript学习——基本包装类型总结

  三种基本包装类型:Boolean,Number,String,分别对应三种基本类型,Boolean,Number,String

  在声明一个基本类型的时候,往往会在后台自动生成一个对应类型的包装类。但是该包装类只存在一行代码的执行瞬间,然后立即被销毁。也就是说不能在运行时为基本类型添加属性和方法。

  例: 

var s1 = "text";

//相当于

var s1 = new String("text");
s1.null;
Boolean:

   创建: var booleanObject = new Boolean(false);

   方法:valueOf(): 返回true或者false,boolean型

               toString(): 返回“true”,或者"false"

   注:布尔表达式所有对象都会被转为true。所以 alert(booleanObject && true); //true


Number:

   创建: var numberObject =  new Number(34);

   方法:valueOf(): 返回基本类型的数值

               toString(): 返回对应数值的字符串,该方法可以接受一个表示基数的参数,告诉它返回几进制数值的字符串。

var num = new Number(10);

alert(num.toString(2)); //1010
alert(num.toString(8));//12
               toFixed(): 返回按照指定小数位返回数值的字符串表示,参数为小数点之后的位数。如果原数更长,则会进行四舍五入

var num = new Number(10.006);

alert(num.toFiexed(2)); //10.01
               toExponential(): 返回以指数表示法表示的数值的字符串形式,参数表示制定输出结果的小数位数。

var num = 10
alert(num.toExponential(1)); //1.0e+1

String:

   创建: var stringObject  =  new String("text");

   属性:length: 表示字符串中包含多少个字符

   方法: 1.charAt(): 返回对应pos的特定字符,参数为基于0的位置。

                2.charCodeAt(): 返回对应位置的字符编码,参数为基于0的位置。

                

var stringValue =  new String("hellow world");
alert(stringValue.charAt(1)); //e
alert(stringValue.charCodeAt(1)); //101

               3.concat(): 用于拼接字符串,返回拼接之后的值,可接受多个参数,并不会改变原字符串的值(但是一般还是用 ‘+’ 实现)

               4.slice(): 如果有一个参数,则为从该位置开始,到最后的字符串。如果有两个参数,则第一个参数为起始位置,第二个是结束位置(不包括)

               5.substring(): 如果有一个参数,则是从该位置开始到最后的字符串。如果有两个参数,则第一个参数为起始位置,第二个是结束位置(不包括)

               6.substr():如果只有一个参数,则是从该位置开始到最后的字符串。如果有两个参数,则第一个参数为起始位置,第二个是子字符串的长度。

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 

             注:当参数是负数的时候,substring()和slice()的处理有区别。当传递的参数是负数的时候,slice()方法会将传入的负数与字符串的长度相加,substr()方法将负数的第一个参数加上字符串的长度,而将负的第二个参数转为0,substring()会将所有的负数参数都变为0.
var str = "hello world";
alert(str.slice(-3));// rld
alert(str.substring(-3));//hello world
alert(str.substr(-3));//rld
alert(str.slice(3,-4)); // lo w
alert(str.substring(3,-4)); //  strstring(3,0)--strstring(0,3) hel
alert(str.substr(3,-4));//"" substr(3,0)
             7. indexof(): 从字符串中搜索给定的子字符串,然后返回子字符串的位置,若找不到返回-1

             9. lastIndexOf():从字符串末尾向前搜索子字符串

以上两个方法可以循环查找所有匹配的子字符串

var text = "lorem ipsum dolor sit amet,consectrtur adipisicing elit";
var position =  new Array();
var pos = text.index("e");

while(pos>-1){
  position.push(pos);
  pos = text("e", pos+1);
}

alert(position); //3,24,32,35,52
            10. trim():用于复制字符串,删除前置及后缀的所有空格(不改变原字符串)

            11. toLowerCase(): 所有字母转为小写

                  toLocaleLowerCase():按地区规则转为小写

                  toUpperCase():所有字母转为大写

                  toLocaleUpperCase():按地区规则转为大写

            12. match():类似于RegExp()的exec()方法,只接受一个参数,正则表达式或者RegExp()对象。返回值是一个数组。

            13.search():类似与match(),参数同match(),返回字符串中第一个匹配项的索引,若没有则返回-1.

            14.replace():替换字符串。第一个参数是RegExp对象或者是一个字符串。第二个参数是字符串或者函数。如果是第一个参数是字符串,则只替换第一个匹配项,若想替换所有的,则第一个参数应该是一个RegExp,并且是/g模式。

var text = "cat,bat,sat,fat";
var result = text.replace("at","ond");
alert(result);// cond,bat,sat,fat

result = text.replace(/at/g,"ond");
alert(result);//cond,bond,sond,fond

                需要注意的是第二个参数是函数的情况。

           

function htmlEscape(text){
    return text.replace(/[<>"&]/g,function(match,pos,originalText){
     switch(match){
        case "<": return "<";
        case ">": return ">";
        case "&": return "&";
        case "\;": return """; 
     }
});
}

alert(htmlEscape("<p class =\"greet\">hellow world!</p>"));
               15.localCompare(): 用于比较两个字符串,如果比参数字符串大,则返回正数; 相等,返回0,比参数字符串小,返回负数





               

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值