字符串方法整理

substring

提取字符串中介于两个指定下标之间的字符

语法:str.substring(indexStart[,indexEnd])

参数:indexStart:要提取的第一个字符在字符串的位置。必需

   indexEnd:比要提取的子串的最后一个字符在字符串中的位置多 1。

(1)如果indexStart等于indexEnd,返回一个空字符串

(2)省略indexEnd,提取字符串直到字符串末尾

(3)任一参数小于0或为NaN,则被当为0

(4)任一参数大于字符串长度,则被当为字符串长度

(5)如果indexStart大于indexEnd,执行效果就像两个参数调换了一样。

举例:

const anyString = 'hello world'
console.log(anyString.substring(3, 3))   //空字符串
console.log(anyString.substring(4))     //'o world'
console.log(anyString.substring(-3, 2))  //'he'
console.log(anyString.substring(6, 20))  //'world'
console.log(anyString.substring(6, 2))   //'llo'

split

使用指定分隔符将字符串分割成子字符串数组。

语法:str.split([separator[, limit]])

参数:separator:分隔符。可以是字符串或者正则表达式。如果省略,则返回的数组为包含整个字符串组成的元素。如果为空字符串,则将原字符串中每个字符以数组形式返回

  limit:一个整数,限定返回的分割片段数量。

参数为正则表达式情况参考 与正则表达式相关的字符串方法

举例:

var str = 'rrewqrewq'
console.log(str.split())    //[ 'rrewqrewq' ]
console.log(str.split(''))  //[ 'r', 'r', 'e', 'w', 'q', 'r', 'e', 'w', 'q' ]
console.log(str.split('', 2))   //[ 'r', 'r' ]

trim

返回一个从两头去掉空白字符的字符串,并不影响字符串本身

var orig = '   foo  ';
console.log(orig.trim()); // 'foo'

charAt

从一个字符串中返回指定的字符

语法:str.charAt(index)

参数:index:介于0和字符串长度减1之间的整数。如果超出该范围,返回空字符串。默认为0

const str = 'fdsafd'
console.log(str.charAt(2))  //'s'

charCodeAt

返回0大屏65535之间的整数,表示给定索引出的UTF-16代码单元

语法:str.charCodeAt(index)

参数:index:同charAt参数。超出范围,返回NaN

const str = 'fdsafd'
console.log(str.charCodeAt(2))  //115

toLowerCase

将调用该方法的字符串值转为小写形式并返回。不会影响字符串本身的值。

语法:str.toLowerCase()

console.log("ALPHABET".toLowerCase())  //alphabet

 

indexOf

返回指定值第一次出现的索引,如果没有,则返回-1。

语法:str.indexOf(searchStr,position)

参数:searchStr:要搜索的字符串

   position:搜素的起始位置,默认为0。可选。

举例:

console.log('hello'.indexOf('l'))   //2
console.log('helol'.indexOf('l', 3))   //4

空字符串不会返回-1,返回查找的起始位置

console.log('hello'.indexOf(''))   //0
console.log('helol'.indexOf('', 3))   //3

lastIndexOf

返回指定值最后一次出现的索引,如果没有,则返回-1

与indexOf方法用法类似,只是返回最后一次出现的索引

举例:

console.log('calla'.lastIndexOf('a'));  //4
console.log('calla'.indexOf('a'));      //1

 

以下为ES6方法

includes

判断当前字符串中是否包含给定的字符串,根据判断结果返回true或false

语法:str.includes(searchStr,position)

参数:searchStr:要搜索的字符串

   position:搜素的起始位置,默认为0。可选。

举例:

let str = 'hello'
console.log(str.includes('o'))      //true
console.log(str.includes('world'))  //false

 

startsWith

判断当前字符串是都以给定的字符串开头,根据判断结果返回true或false

语法:str.startsWith(searchStr,position)

参数:searchStr:要搜索的字符串

   position:搜素的起始位置,默认为0。可选。

举例:

const str = 'abfdahga'
console.log(str.startsWith('a'));       //true
console.log(str.startsWith('b'));       //false
console.log(str.startsWith('b', 1));    //true

 

endsWith

判断当前字符串是都以给定的字符串结尾,根据判断结果返回true或false

语法与参数同 startsWith

不同的是,position默认值为str.length,而不是str.length-1

举例:

const fileName = 'xxx.txt'
if (fileName.endsWith('txt')) {
    console.log('文本文件');
} else if (fileName.endsWith('png')) {
    console.log('图片');
}

padStart与padEnd

用给定字符串补全当前字符串,padStart头部补全,padEnd尾部补全

语法:str.padStart(length,padString)

参数:length:当前字符串需要补全到的目标长度

   padString:用来补全的字符串

举例:

console.log('x'.padStart(5, 'a'));   //'aaaax'
console.log('x'.padEnd(5, 'a'));   //'xaaaa'

//如果原字符串的长度,等于或大于最大长度,则字符串补全不生效,返回原字符串
console.log('xxx'.padEnd(2, 'a'));   //'xxx'

//如果用来补全的字符串与原字符串,两者的长度之和超过了最大长度,则会截去超出位数的补全字符串
console.log('xxx'.padEnd(4, '123456'));   //'xxx1'

//如果省略第二个参数,默认使用空格补全长度
console.log('xxx'.padEnd(5));   //'xxx  '

 

模板字符串

使用反引号`标识,连接字符串。

1、变量放在${变量}

2、所有空格和缩进都会保留在输出中

举例:

let title = '标题'
let content = '内容'

let str1 = '<div> \n <h1>' + title + '</h1> \n <p>' + content + '</p> \n </div>'
console.log(str1)

let str2 = `<div>
    <h1>${title}</h1>
    <p>${content}</p>
</div>`
console.log(str2);

search

参考 与正则表达式相关的字符串方法

match

参考 与正则表达式相关的字符串方法

replace

参考 与正则表达式相关的字符串方法

posted @ 2018-10-22 21:01  懒懒同学  阅读(200)  评论(0编辑  收藏  举报