javascript string 方法总结

字符方法

1、charAt()
接收一个参数,基于0的字符位置。以单字符串的形式返回给定位置的那个字符。

   var stringValue = "hello world";
   console.log(stringValue.charAt(1)); //"e"

2、charCodeAt()
接收一个参数,基于0的字符位置。 返回的是字符编码。

   var stringValue = "hello world";
   console.log(stringValue.charCodeAt(1)); //101

字符串操作方法

1、concat()
用于将一个或多个字符串拼接起来,返回拼接得到的新字符串,不会修改字符串本身的值,只是返回一个基本类型的字符串值。

   var stringValue = "hello ";
   var result = stringValue.concat("world");
   console.log(result); // "hello world"
   console.log(stringValue); // "hello"

2、slice()
截取字符串,只是返回一个基本类型的字符串值,对原始字符串没有任何影响。
如果传两个参数,第一个参数是开始截取的位置,第二个参数是结束截取的位置。

   var stringValue = "hello world";
   console.log(stringValue.slice(3)); //"lo world"
   console.log(stringValue.slice(3,7)); //"lo w"

3、substring()
截取字符串,只是返回一个基本类型的字符串值,对原始字符串没有任何影响。
如果传两个参数,第一个参数是开始截取的位置,第二个参数是结束截取的位置。

   var stringValue = "hello world";
   console.log(stringValue.substring(3)); //"lo world"
   console.log(stringValue.substring(3,7)); //"lo w"

4、substr()
截取字符串,只是返回一个基本类型的字符串值,对原始字符串没有任何影响。
如果传两个参数,第一个参数是开始截取的位置,第二个参数是返回的字符个数。

   var stringValue = "hello world";
   console.log(stringValue.substr(3)); //"lo world"
   console.log(stringValue.substr(3,7)); //"lo worl"

字符串位置方法

1、indexOf()
接收一个参数的时候,返回第一次出现该字符的位置
接收两个参数的时候,第一个是查找的字符,第二个是开始查找的位置。

   var stringValue = "hello world";
   console.log(stringValue.indexOf("o")); //4
   console.log(stringValue.indexOf("o",6)); //7 

2、lastIndexOf()
接收一个参数的时候,返回最后一次出现该字符的位置
接收两个参数的时候,第一个是查找的字符,第二个是开始查找的位置。

   var stringValue = "hello world";
   console.log(stringValue.lastIndexOf("o")); //7
   console.log(stringValue.lastIndexOf("o",6)); //4

trim()方法

这个方法会创建一个字符串的副本,删除前置及后缀的所有空格,然后返回结果。

   var stringValue = " hello world ";
   var trimmedStringValue = stringValue.trim();
   console.log(stringValue); //" hello world "
   console.log(trimmedStringValue); //"hello world"

字符串大小写转换方法

1、toLowerCase()
将字符串转换成小写

   var stringValue = "HELLO WORLD";
   console.log(stringValue.toLowerCase()); //"hello world"

2、toUpperCase()
将字符串转换成大写

   var stringValue = "hello world";
   console.log(stringValue.toUpplerCase()); //"HELLO WORLD"

字符串的模式匹配方法

1、match()
只接受一个参数,要么是一个正则表达式,要么是一个RegExp对象。

   var text = "cat,bat,sat,fat";
   var pattern = /.at/;
   
   var matches = text.match(pattern);
   console.log(maches.index); //0
   console.log(maches[0]); //"cat"

2、search()
唯一参数与match()方法参数相同,search()方法返回字符串中第一个匹配项的索引;如果没有找到匹配项,则返回-1.

   var text = "cat, bat, sat, fat";
   var pos = text.search(/at/);
   console.log(pos); //1

3、replace()
这个方法接收两个参数:第一个参数可以使一个RegExp对象或者一个字符串,第二个参数可以使一个字符串或者一个函数。

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

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

localeCompare()方法

这个方法比较两个字符串,并返回下列值中的一个:

  • 如果字符串在字母表中应该在字符串参数之前,则返回一个负数。
  • 如果字符串等于字符串参数,则返回0
  • 如果字符串在字母表中应该排在字符串参数之后,则返回一个正数。
   var stringValue = "yellow";
   console.log(stringValue.localeCompare("brick")); //1
   console.log(stringValue.localeCompare("yellow")); //0
   console.log(stringValue.localeCompare("zoo")); //-1

fromCharCode()方法

这个方法的任务是接收一个或者多个字符编码,然后将它们转换成一个字符串。

   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、付费专栏及课程。

余额充值