1.字符方法
- 
charAt():接收一个参数,即基于0的字符索引值,返回给定位置的那个字符 
var str = "hello world";
console.log( str.charAt(1) );       //e
- 
charCodeAt():接收的参数同上,返回给定位置的那个字符的字符编码 
var str = "hello world";
console.log( str.charCodeAt(1) );       //101
- ES5还定义了另一个访问个别字符的方法:(IE7及以下返回
undefined) 
var str = "hello world";
console.log( str[1] );       //e
2.字符串操作
- 
concat():类似数组的concat()方法,用于拼接字符串,返回拼接得到的新字符串,不改变原数组;但是实践中使用“+”加号拼接更为常见也更方便,特别是在有多个字符串拼接的情况下 - 
slice():返回被操作字符串的一个子字符串,接收一或两个参数,分别表示起始位置,结束位置 
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;不改变原字符串
- 
substr():返回值同slice();接收一或两个参数,表示起始位置和截取个数 
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);                   //不改变原字符串
- 
substring():返回值和参数都和slice()相同;区别在操作方法 
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.大小写转换
- 
toLowerCase()和toUpperCase()分别将字符串全部转换为小写、大写 
var str = "Hello World";
console.log( str.toLowerCase() );             //hello world  
console.log( str.toUpperCase() );             //HELLO WORLD
console.log(str);                             //Hello World ;不改变原字符串
- 
toLocaleLowerCase()和toLocaleUpperCase()同样也是分别将字符串全部转换为小写、大写,但其是针对特定地区实现 
6.模式匹配方法
- 
match():接收一个参数,要么是正则表达式,要么是RegExp对象 
var str = "cat,bat,sat,fat";
var pattern = /.at/;
console.log( str.match(pattern) );      
//["cat", index: 0, input: "cat,bat,sat,fat"]
//该方法返回一个数组,分别保存了与整个模式匹配的字符串,索引值及每一项匹配的字符串
- 
search():参数与match()相同;返回字符串中第一个匹配项的索引,如果没有匹配项则返回-1,而且该方法总是从字符串开头向后查找 
var str = "cat,bat,sat,fat";
var pattern = /at/;
console.log( str.search(pattern) );      //1
- 
replace():替换字符串的方法,接收2个参数,第一个参数可以是RegExp对象或者一个字符串,第二个参数可以是字符串或者一个函数 
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对象全局匹配替换全部的匹配项
- 
split():基于指定的分隔符讲一个字符串分割成多个子字符串组成的数组;分隔符可以是字符串也可以是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()
- String构造函数本身的一个静态方法,接收一个或多个字符编码,转换为一个字符串
 
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) );
