基本包装类型-String类型

2016-11-07  本文已影响0人  闪电西兰花
1.字符方法
var str = "hello world";
console.log( str.charAt(1) );       //e
var str = "hello world";
console.log( str.charCodeAt(1) );       //101
var str = "hello world";
console.log( str[1] );       //e
2.字符串操作
var str = "hello world";
console.log( str.slice(3) );       //lo world;只有一个参数,表示从起始位置至当前字符串末尾
console.log( str.slice(3,7) );     //lo w;有2个参数时,返回的新字符串不包括结束位置的项
console.log( str.slice(4,1) );   //起始位置小于结束位置时,返回空字符串
console.log( str.slice(-3) );      //rld;参数为负数时,用长度加上该数计算;str.slice( 11+(-3) )
console.log( str.slice(3,-4) );    //lo w;同上,str.slice(3,7)
console.log(str);                  //hello world;不改变原字符串
var str = "hello world";
console.log( str.substr(3) );       //lo world;只有一个参数,表示从起始位置至当前字符串末尾
console.log( str.substr(3,7) );     //lo worl;第2个参数,表示截取长度
console.log( str.substr(-3) );      //rld;第一个参数为负数时,和长度相加;str.substr( 11+(-3) )
console.log( str.substr(3,-4) );    //返回空字符串;第二个参数为负数时,转换为0,因此没有截取长度
console.log(str);                   //不改变原字符串
var str = "hello world";
console.log( str.substring(3) );       //lo world;只有一个参数,表示从起始位置至当前字符串末尾
console.log( str.substring(3,7) );     //lo w;有2个参数时,返回的新字符串不包括结束位置的项
console.log( str.substring(-3) );      //hello world;参数为负数时,都转换为0
console.log( str.substring(3,-4) );    //hel;参数转换为0后变成str.substring(3,0),但是substring会把较小的数作为起始位置,较大的数作为结束位置,因此相当于str.substring(0,3)
console.log(str);                      //不改变原字符串
3.位置方法

参考数组的indexOf()lastIndexOf()方法

4.trim()方法
var str = "   hello world   ";
console.log( str.trim() );             //"hello world"
console.log(str);                      //不改变原字符串
5.大小写转换
var str = "Hello World";
console.log( str.toLowerCase() );             //hello world  
console.log( str.toUpperCase() );             //HELLO WORLD
console.log(str);                             //Hello World ;不改变原字符串
6.模式匹配方法
var str = "cat,bat,sat,fat";
var pattern = /.at/;
console.log( str.match(pattern) );      
//["cat", index: 0, input: "cat,bat,sat,fat"]
//该方法返回一个数组,分别保存了与整个模式匹配的字符串,索引值及每一项匹配的字符串
var str = "cat,bat,sat,fat";
var pattern = /at/;
console.log( str.search(pattern) );      //1
var str = "cat,bat,sat,fat";
console.log( str.replace("at","ond") );     //cond,bat,sat,fat;第一个参数是字符串时,只替换第一个匹配项
console.log( str.replace(/at/g,"ond") );    //cond,bond,sond,fond;使用RegExp对象全局匹配替换全部的匹配项
var str = "cat,bat,sat,fat";
console.log( str.split(",") );      //["cat", "bat", "sat", "fat"]
console.log( str.split(",",2) );    //["cat", "bat"]
7.localeCompare()
var str = "yellow";
console.log( str.localeCompare("brike") );      //1;"brike"在子母表中排在"yellow"之前
console.log( str.localeCompare("yellow") );     //0;"yellow"等于"yellow"
console.log( str.localeCompare("zoo") );        //-1;"zoo"排在"yellow"之后
8.fromCharCode()
console.log( String.fromCharCode(104,101,108,108,111) );      //hello
9.使用数组拼接出如下字符串
 <dl class="product">
   <dt>女装</dt>
   <dd>短款</dd>
   <dd>冬季</dd>
   <dd>春装</dd>
 </dl>
var prod = {
        name: '女装',
        styles: ['短款', '冬季', '春装']
};

function getTpl(data){
    var str = '<dl class="product">\n';
    str += "<dt>" + data.name + "</dt>\n";
    for(var i=0;i<data.styles.length;i++){
         str += "<dd>" + data.styles[i] + "</dd>\n";
    }
    return str += "</dl>";
};
console.log( getTpl(prod) );
上一篇 下一篇

猜你喜欢

热点阅读