JavaScript string字符串对象常见方法

本文总结下几种常见的字符串方法

一、字符方法 chartAt()与charCodeAt()

1.1 chartAt()以单字符字符串的形式返回给定位置的那个字符
1.2 charCodeAt()返回的是字符编码。
        var str="hello world"
        //chartAt()以单字符字符串的形式返回给定位置的那个字符
        console.log(str.charAt(4));//o
        //charCodeAt()返回的是字符编码。
        console.log(str.charCodeAt(4));//111

二、字符串操作方法

 2.1concat() 方法用于连接两个或多个字符串,此方法不改变现有的字符串,返回拼接后的新的字符串。
       var str="hello world"   
     var str1=str.concat('! i am from China');
        console.log(str1);//hello world! i am from China
        //类似于,常用+来拼接
        var msg='! i am from China';
        console.log(str+msg);//hello world! i am from China

2.2 slice(start,[end])

slice() 方法可提取字符串的某个部分,返回一个新的字符串。包括字符串从 start 开始(包括 start)到 end 结束(不包括 end)为止的所有字符。
var str="hello world"; 
console.log(str.slice(0,7))//hello w  空格算一个
2.3 substr(start, [length])
substr() 方法可在字符串中抽取从 start 下标开始的指定数目的字符。返回一个新的字符串,包含从 start(包括 start 所指的字符) 处开始的 length 个字符。如果没有指定 length,那么返回的字符串包含从 start 到该字符串的结尾的字符。
var str="hello world";
console.log(str.substr(2,5));//llo w
 2.4 substring(from, [to])
 substring() 方法用于提取字符串中介于两个指定下标之间的字符.包含star但不包括stop处字符串,to 可选,如果省略该参数,那么返回的子串会一直到字符串的结尾。
var str="hello world";
console.log(str.substring(2,5));//llo

2.5  repeat(n),n是数字参数,代表字符串重复次数.ES6新增

var str="hello world";
 console.log(str.repeat(2));//hello worldhello world

三、字符串位置方法

3.1 indexOf(substr, [start]);

indexOf方法搜索并(如果找到)返回字符串中搜索到的字符或子字符串的索引。如果没有找到,则返回-1。Start是一个可选参数,指定字符串中开始搜索的位置,默认值为0。
 var str="hello world";
console.log(str.indexOf('w'));//6
        console.log(str.indexOf('world'));//6

3.2 lastIndexOf(substr, [start])

      lastIndexOf() 方法返回指定文本在字符串中最后一次出现的索引, 如果未找到,则返回-1。
var str="hello world";
console.log(str.lastIndexOf('l'));//9

3.3 includes()返回布尔值,表示是否找到了参数字符串 ES6新增

 var str="hello world";
console.log(str.includes('he'));//true
console.log(str.includes('ho'));//false

3.4 startsWith()//返回布尔值,表示参数字符串是否在源字符串的头部 ES6新增

 var str="hello world";
console.log(str.startsWith('world'));//false

3.5  endsWith()返回布尔值,表示参数字符串是否在源字符串的尾部ES6新增

var str="hello world";
console.log(str.endsWith('world'));//true

3.6 去空格

trim() 方法会从一个字符串的两端删除空白字符。在这个上下文中的空白字符是所有的空白字符 (space, tab, no-break space 等) 以及所有行终止符字符(如 LF,CR)。
 var str3="    hello world   "
        console.log(str3.length);//18
        var str4=str3.trim();
       console.log(str4);//hello world
       console.log(str4.length);//11

四、字符串大小写转换

4.1 toLowerCase() 方法用于把字符串转换为小写。

var sss="I LIKE PLAYING GAME";
console.log(sss.toLocaleLowerCase());//i like playing game

4.2 toUpperCase()方法用于把字符串转换为大写。

var str="hello world";
console.log(str.toLocaleUpperCase());//HELLO WORLD

 五、字符串的模式匹配方法

5.1match(regexp) 返回数组对象

var intRegex = /[0-9 -()+]+$/;      
    var myNumber = '999';    
    var myInt = myNumber.match(intRegex);    
    console.log(myInt[0]);    //999
   
    var myString = '999 JS Coders';    
    var myInt2 = myString.match(intRegex);    
    console.log(myInt2);//null    

5.2 search(regexp)方法用于检索字符串中指定的子字符串,或检索与正则表达式相匹配的子字符串,如果找到,返回与 regexp 相匹配的子串的起始位置,否则返回 -1。

 var intRegex = /[0-9 -()+]+$/;      
    var myNumber = '999';
var isInt=myNumber.search(intRegex);
    console.log(isInt);//0

5.3replace(regexp/substr, replacetext)

var str="hello world";
 console.log(str.replace(/world/i,"China"));//hello China

5.4 split(delimiter, [limit])

split() 方法用于把一个字符串分割成字符串数组,返回一个字符串数组返回的数组中的字串不包括 delimiter自身。可选的“limit”是一个整数,允许各位指定要返回的最大数组的元素个数。
 var aa="asd,ffg,hjkl"
     console.log(aa.split(',',3));//["asd", "ffg", "hjkl"]

 

 

posted @ 2019-10-08 18:02  smile小吴  阅读(531)  评论(0编辑  收藏  举报