JS---String

445 阅读5分钟

charAt(index)

返回在指定位置的字符。

  let str = 'hello word';
  let data = str.charAt(1);  // e

charCodeAt(index)

返回在指定的位置的字符的 Unicode 编码。

  let str= 'hello word'
  let data = str.CodeAt(1); // 101

String.fromCharCode(n1, n2, ..., n)

将 Unicode 编码转为一个字符,不能识别码点大于0xFFFF的字符。

  let data = String.fromCharCode(101);  // e
  let data2 = String.fromCharCode(101, 101)  // ee

codePointAt(index: Number)

JavaScript 内部,字符以 UTF-16 的格式储存,每个字符固定为2个字节。对于那些需要4个字节储存的字符(Unicode 码点大于0xFFFF的字符),JavaScript 会认为它们是两个字符。

方法会正确返回 32 位的 UTF-16 字符的码点。对于那些两个字节储存的常规字符,它的返回结果与charCodeAt()方法相同。

  let s = "𠮷";
  s.length // 2
  s.charAt(0) // ''
  s.charAt(1) // ''
  s.charCodeAt(0) // 55362
  s.charCodeAt(1) // 57271
  
  let s = '𠮷a';
  s.codePointAt(0) // 134071
  s.codePointAt(1) // 57271
  s.codePointAt(2) // 97

fromcCodePoint()

将 Unicode 编码转为一个字符,可以识别大于0xFFFF的字符

  String.fromCharCode(0x20BB7);  // "ஷ"
  String.fromCodePoint(0x20BB7);  // "𠮷"

indexOf(str: String, start?: Number): Number

返回某个指定字符在字符串首次出现的位置,第二个参数 start规定在字符串中开始检索的位置,默认是从首字符开始。如果没找到匹配的字符串则返回 -1

let str = 'hello word'
str.indexOf('h');  // 0
str.indexOf('o');  // 4
str.indexOf('o', 5);  // 7

lastIndexOf(str: String, start?: Number): Number

返回一个指定的字符串值最后出现的位置,如果指定第二个参数 start,则在一个字符串中的指定位置从后向前搜索,默认是从字符串结尾开始。如果没找到匹配的字符串则返回 -1

let str = 'hello word'
str.lastIndexOf('o') // 7
str.lastIndexOf('o', 6)  // 4

includes(str: String): Boolean

方法用于判断字符串是否包含指定的子字符串

  let str = 'hello wrod';
  str.includes('hello') // true

startsWith(str: String, start?: Number): Boolean

方法用于检测字符串是否以指定的子字符串开始。 参数start 是开始检索的位置、

  'hello word'.startsWith('hello')  // true
  'hello word'.startsWith('hello',1)  // false

endsWith(str: String, end?: Number): Boolean

方法用于检测字符串是否以指定的子字符串开始。 参数end 是检索结束位置、

  'hello word'.endsWith('word')  // true
  'hello word'.endsWith('hello', 7)  // false

repeat(count: Number): String

返回字符串复制指定次数后的值

  'hello word'.repeat(2);  // 'hello wordhello word'

replace(oldValue: String | RegExp, newValue: String): String

用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串

  let str = 'hello word hello word';
  str.replace('word', 'boy');  // 'hello boy hello word' 只会替换一次
  str.replace(/word/, 'boy');  // 'hello boy hello word' 只会替换一次
  str.replace(/word/g, 'boy');  // 'hello boy hello boy' 替换多次

replaceAll(oldValue, newValue)

replace()类似,不同之处是replaceAll()可以一次性替换所有匹配。

  'aabbcc'.replaceAll('b', '_')  // 'aa__cc'

padStart(length: Number, value?: String): String

从头部开始补全字符串长度,如果原字符串的长度,等于或大于最大长度,则字符串补全不生效,返回原字符串。
如果省略第二个参数,默认使用空格补全长度。

'ooo'.padStart(6, 'ab');  // 'abaooo'
'ooo'.padStart(6);  // '   ooo'

padEnd(length: Number, value?: String): String

从尾部部开始补全字符串长度,如果原字符串的长度,等于或大于最大长度,则字符串补全不生效,返回原字符串。
如果省略第二个参数,默认使用空格补全长度。

'ooo'.padStart(6, 'ab');  // 'oooaba'
'ooo'.padStart(6);  // 'ooo   '

substr(start: Number, length?: Number): String

方法可在字符串中抽取从开始下标开始的指定数目的字符。
会将第一个负参数与字符串长度相加,第二个负参数转化为 0

  '0123'.substr(1,2) // '12'
  '0123'.substr(1,5) // '123'
  '0123'.substr(1) // '123'
  '0123'.substr(2, -5)  // ''
  '0123'.substr(-3,2)  // '12'

substring(from: Number, to?: Number): String

提取字符串中两个指定的索引号之间的字符。
将所有的负参数转化为 0
以两个参数中较小一个作为起始位置,较大的参数作为结束位置

  '0123'.substring(1,3)  // '12'
  '0123'.substring(1)  // '123'
  '0123'.substring(1, 5)  // '123'
  '0123'.substring(2,-5)  // '01'
  '0123'.substring(-2, 1) // '2'

slice(start: Number, end?: Number): String

方法可提取字符串的某个部分,并以新的字符串返回被提取的部分。
会将所有的负数与字符串的长度相加

 '0123'.slice(1,2);  // '1'
 // '0123'.slice(1); 相当于 '0123'.slice(1,4); 4代表字符串的length
 '0123'.slice(1);  // '123' 
 // 会将所有的负数与字符串的长度相加
 '0123'.slice(1,-2);  // '1'
 '0123'.slice(1,-5);  // ''
 '0123'.slice(-1);  // '3'
 '0123'.slice(-1,-2);  // ''

toLowerCase(): String

把字符串转换为小写。

  'aaBb'.toLowerCase(); //'aabb'

toUpperCase(): String

把字符串转换为大写。

  'aaBb'.toUpperCase(); //'AABB'

split(separator?: String | RegExp, limit?: Number): String[]

将字符串分割成数组

  'aabbccbbdd'.split('bb',2);  // ['aa', 'cc']
  'aabbccbbdd'.split(/bb/);  // ['aa', 'cc', 'dd']

contact(string1: String, string2: String, ..., stringX: String): String

将多个字符串连接成一个字符串

  let [str1, str2] = ['str1, 'tr2'];
  let str3 = str1.contact(str2);  // 'str1str2'

search(separator: String | RegExp): Number

查找指定字符串或者与正则表达式相匹配的值的起始位置,如果没有返回-1

  'aabb'.search('c'); // -1
  'aabb'.search('a'); // 0
  'aabb'.search(/abb/); // 1

match(regexp: RegExp): String[]

  "The rain in SPAIN stays mainly in the plain".match(/ain/gi);  // ['ain', 'AIN', 'ain', ain]