JavaScript中关于字符串操作总结

因为关于JavaScript中的字符串(String类型)的操作经常是忘记或者是混淆了,所以今天借着这个机会进行总结一番。

String类型都有valueOf(),toLocaleString(),toString()方法,都返回对象所表示的基本字符串。String类型都有一个length属性,表示字符串的长度

1. 字符方法
  • charAt()charCodeAt()
    这两个方法都接受一个基于0的字符位置的参数。
    charAt()方法以单字符串的形式返回给定位置的那个字符。
var str = 'hello world'
console.log(str.charAt(1)) //e

charCodeAt()返回的是该位置的字符串编码

var str = 'hello world'
console.log(str.charCodeAt(1)) //101

TIP: charCodeAt和charAt中如果传入的参数为负数或者大于该字符串的长度,charAt()返回的是空字符(””),charCodeAt()返回的是NAN。

2. 字符串操作方法
  • concat()
    用于将一个或者多个字符串拼接起来得到一个新的字符串。并不改变原来的字符串。
    concat()可以接受任意多个参数,也就是可以拼接任意多个字符串。
var str = 'hello '
var res = str.concat('world', '!')
console.log(res) //hello world
console.log(str) //hello

TIP: concat中传入的参数如果是非字符串,则会先调用toString方法尝试转换成字符串然后进行拼接

  • slice() substr() substing()
    以上三个方法都会返回被操作字符串的子字符串,而且都会接受一个或者两个参数。
    第一个参数指定子字符串的开始位置,第二个参数表示子字符串到哪里结束。
    slice()和substring()的第二个参数指定的是子字符串最后一个字符串后面的位置。
    substr()指定的是返回的字符串个数。
    以上三个方法和concat()不会修改字符串本身的值,只会返回一个基本类型的字符串。

slice()方法会将传入的负值与字符串的长度相加。substr()方法将负的第一个参数加上字符串的长度,而负的第二个参数转为0。substring()会将所有负值参数都转换为0,如果第二个参数比第一个大的话,那么会将两个参数位置进行交换

var str = 'hello world'
str.slice(3) // 'lo world'
str.substring(3) // 'lo world'
str.substr(3) // 'lo world'
str.slice(3, 7) //'lo w'
str.substring(3, 7) //'lo w'
str.substring(3, 7) //'lo worl'

var str = 'hello world'
str.slice(-3) //'rld' 从后往前数
str.substring(-3) //'hello world',会转为0,然后截取到最后就返回整个字符串了
str.substr(-3) //'rld',第一个参数从后往前数
str.slice(3, -4)// 'lo w',若第二个参数小于第一个参数,则返回空字符串
str.substring(3, -4) //'hel', 负数会转为0,然后交换0和3的位置
str.substr(3, -4) // ""会将负数转为0,然后如果第二个小于第一个则会返回空字符串 
3. 字符串位置方法
  • indexOf() lastIndexOf()
    这两个方法都是从一个字符串中搜索出给定的子字符串,然后返回子字符串的位置。如果没有找到该子字符串则返回-1
    这两个方法的区别在于indexOf()从字符串的开头向后搜索子字符串,lastIndexOf()方法是从字符串的末尾进行搜索。
var str = 'hello world'
console.log(str.indexOf('o')) //4
console.log(str.lastIndexOf('o')) //7

indexOf()和lastIndexOf()还可以接受第二个参数表示从第几位进行搜索。

  • trim()方法
    该方法会创建一个字符串的副本,删除前置以及后缀的所有空格,然后返回结果。
var str = '   hello world  '
console.log(str.trim()) //'hello world'
  • toLowerCase(), toLocaleLowerCase(), toUpperCase(), toLocaleUppercase()
    以上顾名思义是对字符串进行大小写的转换,特殊的是toLocaleLowerCase(),toLocaleUpperCase()是针对特定地区的实现。所以一般在不知道在哪种语言的环境中,使用针对地区的方法更精确一点。

  • match()
    该方法与RegExpexec()方法相同。
    该方法接受一个参数,要么是一个正则表达式,要么是一个RegExp对象。

var str = 'cat, bat, sat, fat'
var pattern = /.at/
var matches = str.match(pattern)
console.log(matches.index) //0
console.log(matches[0]) //'cat'
console.log(pattern.lastIndex) // 0

如上,match()方法返回一个数组。

  • search()
    该方法和match()方法的参数相同(字符串或者是RegExp对象指定的正则表达式)。
    该方法返回字符串中第一个匹配项的索引,如果没有找到匹配项则返回-1
var test = 'cat, bat, sat, fat'
var pos = test.search(/at/)
console.log(pos) // 1
  • replace()
    该方法可以用来进行替换字符串,方法可以接受两个参数。
    第一个参数可以是一个正则表达式或者是一个字符串,第二个参数可以是一个字符串或者是一个函数
    如果第一个是参数是一个字符串,那么只会替换第一个子字符串,如想要替换所有的子字符串,那么需要提供一个正则表达式,并且需要指定全局。
var text = 'cat, bat, sat, fat'
var res = text.replace('at', 'ond')
console.log(res) // 'cond, bat, sat, fat'
res = text.replace(/at/g, ''ond")
console.log(res) // "cond, bond, sond, fond"
  • split()
    该方法大家应该熟悉,会根据指定的分隔符将一个字符串分割成多个子字符串,然后组成一个数组。

  • localeCompare()
    该方法是比较两个字符串。
    如果字符串在字母表中应该排在字符串参数之前,则返回一个负数。
    如果字符串等于字符串参数则返回0。
    如果字符串在字母表中应该排在字符串参数之后,则返回一个正数。

  • fromCharCode()
    该方法是String构造函数的一个静态方法。该方法接受一个或者多个字符串编码,然后转换为一个字符串。

String.fromCharCode(104, 101, 108, 108, 111) //hello

以上就是关于JavaScript中字符串的一些基本操作。

    原文作者:FeRookie
    原文地址: https://www.jianshu.com/p/9fb48807b5e5
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞