字符串的方法?

1.字符串位置方法

indexOf()方法返回字符串中指定文本首次出现的索引(位置)

var str = "The full name of China is the People's Republic of China.";
var pos = str.indexOf("China");
console.log(pos)    //17

lastIndexOf()  方法返回指定文本在字符串中最后一次出现的索引

var str = "The full name of China is the People's Republic of China.";
var pos = str.lastIndexOf("China");
console.log(pos)  // 51

如果未找到文本, indexOf() 和 lastIndexOf() 均返回 -1。

var str = "The full name of China is the People's Republic of China.";
var pos = str.indexOf("USA");
console.log(pos) //-1

2.字符方法

动态方法:1、str.charAt(index);  返回子字符串,index为字符串下标,index取值范围[0,str.length-1]

动态方法:2、str.charCodeAt(index);  返回子字符串的unicode编码,index取值范围同上

静态方法:3、String.fromCharCode(num1,num2,...,numN);  根据unicode编码返回字符串

/* 
  charAt方法和charCodeAt方法都接收一个参数,基于0的字符位置 
  charAt方法是以单字符字符串的形式返回给定位置的那个字符 
  charCodeAt方法获取到的不是字符而是字符编码 
   */
    var str="hello world"; 
    console.log(str.charAt(1));//e 
    console.log(str.charCodeAt(1));//101 
    //还可以使用方括号加数字索引来访问字符串中特定的字符 
    console.log(str[1]);//e 

3.字符串操作方法

concat()连接两个或多个字符串,返回连接后的字符串

var str="hello "; 
var res=str.concat("world"); 
console.log(res);//hello world 
console.log(str);//hello  这说明原来字符串的值没有改变 
var res1=str.concat("nihao","!"); 
console.log(res1);//hello nihao!  说明concat方法可以接收任意多个参数 
 //虽然concat方法是专门用来拼接字符串的,但是实践中我们使用最多的还是加操作符+,因为其简易便行 

4.截取字符串

slice()方法、substring()方法、substr()方法

str.substring(start,end);  两个参数都为正数,返回值:[start,end) 也就是说返回从start到end-1的字符

str.slice(start,end);  两个参数可正可负,负值代表从右截取,返回值:[start,end) 也就是说返回从start到end-1的字符

str.substr(start,length);  start参数可正可负,负数代表从右截取

 /* 
    slice方法:第一个参数指定子字符串开始位置,第二个参数表示子字符串最后一个字符后面的位置 
    substring方法:第一个参数指定子字符串开始位置,第二个参数表示子字符串最后一个字符后面的位置 
    substr方法:第一个参数指定子字符串开始位置,第二个参数表示返回的字符个数 
    这三个方法都会返回被操作字符串的一个子字符串,都接收一或两个参数 
    如果没有给这些方法传递第二个参数,则将字符串的长度作为结束位置。这些方法也不会修改字符串本身,只是返回一个基本类型的字符串值 
     */
    var str="hello world"; 
    console.log(str.slice(3));//lo world 
    console.log(str.substring(3));//lo world 
    console.log(str.substr(3));//lo world 
    console.log(str.slice(3,7));//lo w  7表示子字符串最后一个字符后面的位置  简单理解就是包含头不包含尾 
    console.log(str.substring(3,7));//lo w 
    console.log(str.substr(3,7));//lo worl 7表示返回7个字符 
  
    console.log(str.slice(3,-4));//lo w  -4+11=7表示子字符串最后一个字符后面的位置  简单理解就是包含头不包含尾 
    console.log(str.substring(3,-4));//hel  会转换为console.log(str.substring(3,0)); 
    //此外由于这个方法会将较小数作为开始位置,较大数作为结束位置,所以相当于调用console.log(str.substring(0,3)); 
    console.log(str.substr(3,-4));//""空字符串 
    console.log(str.substring(3,0)); 
  • str.indexOf(searchString,startIndex);  返回子字符串第一次出现的位置,从startIndex开始查找,找不到时返回-1
  • str.lastIndexOf(searchString,startIndex);  从由往左找子字符串,找不到时返回-1
    var str="hello world"; 
    console.log(str.indexOf("o"));//4 
    console.log(str.lastIndexOf("o"));//7 
    console.log(str.indexOf("o",6));//7 
    console.log(str.lastIndexOf("o",6));//4 

5.trim方法

trim() 方法用来删除字符串前后的空格

    var str="   hello world   "; 
    console.log('('+str.trim()+')');//(hello world) 
    console.log('('+str+')');//(   hello world   ) 

6.字符串大小写转换方法

 str.toLowerCase()

 str.toUpperCase()

7.字符串分割成数组

split(separator,limit);  参数1指定字符串或正则,参照2指定数组的最大长度

    var str="red,blue,green,yellow"; 
    console.log(str.split(","));//["red", "blue", "green", "yellow"] 
    console.log(str.split(",",2));//["red", "blue"]  第二个参数用来限制数组大小 
    console.log(str.split(/[^\,]+/));// ["", ",", ",", ",", ""] 

8.localeCompare方法

/* 
    这个方法用于比较两个字符串 
    1.如果字符串在字母表中应该排在字符串参数之前,则返回一个负数 
    1.如果字符串等于字符串参数,则返回0 
    1.如果字符串在字母表中应该排在字符串参数之后,则返回一个正数 
     */
    var str="yellow"; 
    console.log(str.localeCompare("brick"));//1 
    console.log(str.localeCompare("yellow"));//0 
    console.log(str.localeCompare("zoo"));//-1 

9.fromCharCode方法

    /* 
    fromCharCode方法是接收一或多个字符编码,然后将其转换为字符串 
    fromCharCode方法是String构造函数的一个静态方法 
     */
    console.log(String.fromCharCode(104,101,108,108,111));//hello 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值