一. length

JS中的字符串有一个length属性,该属性可以用来获取字符串的长度

1
2
const str = "hello"
str.length //输出结果5

二.获取字符串指定位置的值

  • charAt() 方法获取到的是指定位置的字符
  • charCodeAt()方法获取的是指定位置字符的Unicode值

charAt()

1
2
3
4
5
const str = "hello"
str.chatAt(1) // 输出结果:e
str[1] // 输出结果:e
str.charAt(5) // 输出结果:""
str[5] // 输出结果:undefine

charCodeAt()

1
2
let str = "abcdefg"
console.log(str.charCodeAt(1)) // "b"--> 98

三.检索字符串是否包含特定序列

  1. indexOf()

查找某个字符,有则返回第一次匹配到的位置,否则返回-1

1
2
3
4
5
6
7
8
9
string.indexOf(searchvalue,fromindex)

searchvalue:必需,规定需检索的字符串值
fromindex:可选的整数参数,规定在字符串开始检索的位置。合法取值范围是0-string.length-1.如省略,则从字符串的首字符开始检索

let str = "abcdefgabc"
console.log(str.indexOf("a")) // 输出结果:0
console.log(str.indexOf("z")) // 输出结果:-1
console.log(str.indexOf("c",4)) //输出结果:9
  1. lastIndexOf()

查找某个字符,有则返回最后一次匹配的位置,否则返回-1

1
2
3
let str = “abcabc”
console.log(str.lastIndexOf("a")) // 输出结果:3
console.log(str.lastIndexOf("z")) // 输出结果:-1
  1. includes()

该方法用于判断字符串是否包含指定的子字符串,如果找到指定的子字符串,返回true,否则返回false

1
2
3
4
5
6
7
8
9
string.includes(searchvalue,start)

searchvalue:必需,要查找的字符串
start:可选,设置从那个位置开始查找,默认为0

let str = "Hello world"
str.includes("o") // 输出结果:true
str.includes("z") // 输出结果:false
str.includes("e",2) // 输出结果:false
  1. startsWith()

该方法用于检测字符串是否以指定的子字符串开始。如果是以指定的子字符串开头返回 true,否则 false。

1
2
3
4
5
6
let str = 'Hello world!';

str.startsWith('Hello') // 输出结果:true
str.startsWith('Helle') // 输出结果:false
str.startsWith('wo', 6) // 输出结果:true

  1. endsWith()

该方法用来判断当前字符串是否是以指定的子字符串结尾。如果传入的子字符串在搜索字符串的末尾则返回 true,否则将返回 false。

1
2
3
4
5
6
7
8
9
10
string.endsWith(searchvalue, length)

searchvalue:必需,要搜索的子字符串;
length: 设置字符串的长度,默认值为原始字符串长度 string.length。

let str = 'Hello world!';

str.endsWith('!') // 输出结果:true
str.endsWith('llo') // 输出结果:false
str.endsWith('llo', 5) // 输出结果:true

四、连接多个字符串concat()

1
2
3
4
5
6
string.concat(string1, string2, ..., stringX)

let str = "abc";
console.log(str.concat("efg")); //输出结果:"abcefg"
console.log(str.concat("efg","hijk")); //输出结果:"abcefghijk"

五、字符串分割成数组split()

用于把一个字符串分割成字符串数组,该方法不会改变原始字符串

1
2
3
4
5
6
7
8
string.split(separator,limit)

separator:必需。字符串或正则表达式,从该参数指定的位置分割string
limit:可选,该参数可指定返回的数组的最大长度,如果设置了该参数,返回的子字符串不会多于这个参数指定的数组;如果没有设置该参数,整个字符串都会被分割,不考虑他的长度

let str = "abcdef";
str.split("c"); // 输出结果:["ab", "def"]
str.split("", 4) // 输出结果:['a', 'b', 'c', 'd']

六、截取字符串

  1. slice()

用于提取字符串的某一部分,并以新的字符串返回提取的部分,该方法返回的子串包括开始处的字符,但不包括结束处的字符。

1
2
3
4
5
6
7
8
9
10
11
string.slice(start,end)

start:必需,要截取的片段的起始下标,第一个字符串位置为0,如果为负数,则从尾部开始截取
end: 可选,要截取的片段结尾的下标,若未指定此参数,则要提取的字串包含start到原字符串结尾的字符串。如果是负数,则从字符串的尾部开始算起的位置

let str= "abcdefg"
str.slice(1,6) // 输出结果:“bcdef”
str.slice(1) // 输出结果:“bcdefg”
str.slice() // 输出结果:“abcdefg”
str.slice(-2) //输出结果:“fg”
str.slice(6,1) //输出结果:""

2.substr()
用于在字符串中抽取从开始下标开始的指定数目的字符。

1
2
3
4
5
6
7
8
9
string.substr(start,length)

start:必需,要抽取的字串的起始下标。必须是数值。如果是负数,则该参数声明从字符串的尾部开始算起的位置
length:可选,子串中字符数。如果省略了该参数,则返回从stringObject的开始位置到结尾的字串

let str = "abcdefg"
str.substr(1,6) //输出结果:“bcdefg”
str.substr(1) // 输出结果:“bcdefg”
str.substr(-1) //输出结果:“g”

3.substring()
用于提取字符串中介于两个指定下标之间的字符

1
2
3
4
5
6
7
8
9
10
11
12
string.substring(from,to)

from:必需,一个非负的整数,规定要提取的子串的第一个字符在string的位置
to:可选,一个非负的整数,比要提取的子串的最后一个字符在string中的位置多1。如省略,返回的子串会一直到字符串的结尾


let str = "abcdefg"
str.substring(1,6) //输出结果:“bcdef”
str.substring(1) //输出结果:“bcdefg”
str.substring() // 输出结果:“abcdefg”
str.substring(6,1) // 输出结果:“bcdef”
str.substring(-1) // 输出结果:"abcdefg"

七、字符串大小写转换

  1. toLowerCase()
    用于把字符串转换成小写
1
2
let str = "adABDndj"
str.toLowerCase(); //输出结果:“adabdndj”

2.toUpperCase()
用于将字符串转换成大写

1
2
let str = “asdfGH”
str.toUpperCase(); //输出结果:“ASDFGH”

八、字符串模式匹配

1.replace()
用于在字符串中用一些字符串替换名一些字符串,或者替换一个与正则表达式匹配的子串

1
2
3
4
5
6
7
8
9
10
11
string.replace(searchvalue,newvalue)

searchvalue:必需,规定子字符串或要替换的模式的RwgExp对象。如果该值是一个字符串,则将它作为要检索的直接量文本模式,而不是首先被转换成RegExp对象
newvalue:必需,一个字符串的值

let str = “abcdef”
str.replace("c","a") // 输出结果:abadef

let str="Mr Blue has a blue house and a blue car";
str.replace(/blue/gi, "red"); // 输出结果:'Mr red has a red house and a red car'

  1. match()

用于在字符串内检索特定的值,或找到一个或多个正则表达式的匹配

1
2
3
4
string.match(regexp)

let str = "abcdef"
console.log(str.match("c")) // // ["c", index: 2, input: "abcdef", groups: undefined]
  1. search()

用于检索字符串中指定的子字符串,或检索与正则表达式相匹配的子字符串。只会返回第一次匹配成功的结果;如果没有找到任何匹配的子串,则返回 -1。

1
2
3
4
5
string.search(searchvalue)

let str = "abcdef";
str.search(/bcd/) // 输出结果:1

九、移除字符串首尾空白符

  1. trim()

用于移除字符串首尾空白符,该方法不会改变原始字符串

1
2
3
let str = "  abcdef  "
str.trim() // 输出结果:"abcdef"

  1. trimStart()

trimStart() 方法的的行为与trim()一致,不过会返回一个从原始字符串的开头删除了空白的新字符串,不会修改原始字符串:

1
2
3
const s = '  abc  ';

s.trimStart() // "abc "
  1. trimEnd()

trimEnd() 方法的的行为与trim()一致,不过会返回一个从原始字符串的结尾删除了空白的新字符串,不会修改原始字符串:

1
2
3
4
const s = '  abc  ';

s.trimEnd() // " abc"

十、获取字符串本身

  1. valueOf()

返回某个字符串对象的原始值,该方法通常由 JavaScript 自动进行调用,而不是显式地处于代码中。

1
2
let str = "abcdef"
console.log(str.valueOf()) // "abcdef"
  1. toString()

返回字符串对象本身

1
2
let str = "abcdef"
console.log(str.toString()) // "abcdef"

十一、重复一个字符串repeat()

返回一个新字符串,表示将原字符串重复n次:

1
2
3
'x'.repeat(3)     // 输出结果:"xxx"
'hello'.repeat(2) // 输出结果:"hellohello"
'na'.repeat(0) // 输出结果:""

注意:如果参数是小数,向下取整;如果参数是负数或Infinity,会报错

十二、补齐字符串长度

  1. padStart()

用于头部补全。该方法有两个参数,其中第一个参数是一个数字,表示字符串补齐之后的长度;第二个参数是用来补全的字符串。

1
2
3
4
5
6
7
'x'.padStart(1, 'ab') // 'x'
'x'.padStart(5, 'ab') // 'ababx'
'x'.padStart(4, 'ab') // 'abax'
'x'.padStart(4) // ' x'
"1".padStart(3, '0') // 输出结果: '001'
"15".padStart(3, '0') // 输出结果: '015'

  1. padEnd()

用于尾部补全。该方法也是接收两个参数,第一个参数是字符串补全生效的最大长度,第二个参数是用来补全的字符串:

1
2
3
'x'.padEnd(5, 'ab') // 'xabab'
'x'.padEnd(4, 'ab') // 'xaba'

十三、字符串转为数字

1.parseInt()

用于可解析一个字符串,并返回一个整数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
parseInt(string, radix)

string:必需,要被解析的字符串
radix:可选,表示要解析的数字的基数。

parseInt("10"); // 输出结果:10
parseInt("17",8); // 输出结果:15 (8+7)
parseInt("010"); // 输出结果:10 或 8

parseInt("0x10") // 输出结果:16


parseInt("50", 1) // 输出结果:NaN
parseInt("50", 40) // 输出结果:NaN


parseInt("40 4years") // 输出结果:40

parseInt("new100") // 输出结果:NaN

parseInt(" 60 ") // 输出结果: 60

  1. parseFloat(string)

可解析一个字符串,并返回一个浮点数。该方法指定字符串中的首个字符是否是数字。如果是,则对字符串进行解析,直到到达数字的末端为止,然后以数字返回该数字,而不是作为字符串。

1
2
3
4
5
6
parseFloat("10.00")      // 输出结果:10.00
parseFloat("10.01") // 输出结果:10.01
parseFloat("-10.01") // 输出结果:-10.01
parseFloat("40.5 years") // 输出结果:40.5