字符串方法

67 阅读4分钟

字符串方法

  • charAt(x)
  • charCodeAt(x)
  • concat(v1, v2..)
  • fromCharCode(number1, number2)
  • indexOf(substr, [start])
  • lastIndexOf(substr, [start])
  • match(regexp)
  • replace(regexp/substr, text)
  • search(regexp)
  • slice(start, [end])
  • split(delimiter, [limit])
  • substr(start, [length])
  • substring(from, [to])
  • toLowerCase()
  • toUpperCase()
  • includes()
  • endsWith()
  • repeat()
  • valueOf()
  • trim()

1. charAt(x)

charAt(x)返回字符串中x位置的字符,下标从 0 开始。

console.log('ganshibiao'.charAt(3));  // s

2. charCodeAt(x)

charCodeAt(x)返回字符串中x位置处字符的unicode值。

console.log('ganshibiao'.charCodeAt(1))  // 97

3. concat(v1, v2..)

concat() 方法用于连接两个或多个字符串,此方法不改变现有的字符串,返回拼接后的新的字符串。

let name = 'shibiao';
let names = ['shi', 'biao'];

console.log(name);  // shibiao
console.log(name.concat('gan'));  // shibiaogan
console.log(name.concat(names))  // shibiaoshi,biao
console.log(name.concat(...names))  // shibiaoshibiao
console.log(name.concat('gan', names))  // shibiaoganshi,biao

4. fromCharCode(c1,c2)

String.fromCharCode(number1, number2)转换一组Unicode值转换为字符。

console.log(String.fromCharCode(103, 97, 110, 115, 104, 105, 98, 105, 97, 111))  // ganshibiao

5. indexOf(substr, [start])

string.indexOf()方法搜索并(如果找到)返回字符串中搜索到的字符或子字符串的索引。如果没有找到,则返回-1。Start是一个可选参数,指定字符串中开始搜索的位置,默认值为0。

let name = 'ganshibiao';
console.log(name.indexOf('gan'));  // 0
console.log(name.indexOf('gan', 1));  // -1

6. lastIndexOf(substr, [start])

string.lastIndexOf() 方法返回指定文本在字符串中最后一次出现的索引, 如果未找到,则返回-1。 Start是一个可选参数,指定字符串中开始搜索的位置, 默认值为string.length:-1。

let name = 'ganshibiao';
console.log(name.lastIndexOf('gan'));  // 0
console.log(name.lastIndexOf('gan', 1000)); // 0
console.log(name.lastIndexOf('ganl', 1000)); // -1

7. match(regexp)

string.match(regexp) 方法根据正则表达式在字符串中搜索匹配项。如果没有找到匹配项,则返回一个信息数组或null。

let name = 'ganshibiao';
console.log(name.match(/\w+/));  // ['ganshibiao', index: 0, input: 'ganshibiao', groups: undefined]

8. replace(regexp/substr, text)

string.replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的字符串,不会改变原字符串。

let name = 'gan,shi,biao';
console.log(name.replace(',', ''));  // ganshi,biao
console.log(name);  // gan,shi,biao
console.log(name.replace(/\w+/, '')); // ,shi,biao

console.log(name.replaceAll(',', ''));  // ganshibiao
console.log(name.replaceAll(/\w+/, ''));  // 报错:Uncaught TypeError: String.prototype.replaceAll called with a non-global RegExp argument
console.log(name);  // gan,shi,biao

9. search(regexp)

string.search() 方法用于检索字符串中指定的子字符串,或检索与正则表达式相匹配的子字符串,如果找到,返回与 regexp 相匹配的子串的起始位置,否则返回 -1。

console.log(number.search(/[0-9 -()+]+$/));  // 0

10.slice(start, [end])

slice() 方法可提取字符串的某个部分,返回一个新的字符串。包括字符串从 start 开始(包括 start)到 end 结束(不包括 end)为止的所有字符。

let text = 'excellent';
console.log(text.slice(0, 4)); // exce
console.log(text.slice(2, 4)); // ce

11.split(delimiter, [limit])

split() 方法用于把一个字符串分割成字符串数组,返回一个字符串数组返回的数组中的字串不包括 delimiter自身。 可选的“limit”是一个整数,允许各位指定要返回的最大数组的元素个数。

let name = 'gan,shi,biao';
console.log(name.split(','));  // ['gan', 'shi', 'biao']

12.substr(start, [length])

substr() 方法可在字符串中抽取从 start 下标开始的指定数目的字符。返回一个新的字符串,包含从 start(包括 start 所指的字符) 处开始的 length 个字符。如果没有指定 length,那么返回的字符串包含从 start 到该字符串的结尾的字符。

var text = 'excellent';
console.log(text.substr(0,4));  // exce
console.log(text.substr(2,4));  // cell

13.substring(from, [to])

substring() 方法用于提取字符串中介于两个指定下标之间的字符,方返回的子串包括 start 处的字符,但不包括 stop 处的字符,to 可选,如果省略该参数,那么返回的子串会一直到字符串的结尾。

var myString = 'javascript rox';
myString = myString.substring(0,10);
console.log(myString);  // javascript

14.toLowerCase()

toLowerCase() 方法用于把字符串转换为小写。

var myString = 'JAVASCRIPT ROX';
myString = myString.toLowerCase();
console.log(myString);  // javascript rox

15.toUpperCase()

toUpperCase() 方法用于把字符串转换为大写。

//toUpperCase()

var myString = 'javascript rox';
myString = myString.toUpperCase();
console.log(myString);  // JAVASCRIPT ROX
  1. includes()

includes() 方法用于检查字符串是否包含指定的字符串或字符。

var string = 'Hello, welcome to edureka';
console.log(string.includes('edureka'));  // true
  1. endsWith()

endsWith()函数检查字符串是否以指定的字符串或字符结束。

var mystr = 'List of javascript functions';
console.log(mystr.endsWith('functions'));  // true
  1. repeat()

repeat() 构造并返回一个新字符串,该字符串包含被连接在一起的指定数量的字符串的副本。

var string = 'Welcome to Edureka';
console.log(string.repeat(2));  // Welcome to Edureka Welcome to Edureka
  1. valueOf()

valueOf() 方法返回一个String对象的原始值(primitive value),该值等同于String.prototype.toString()。

var mystr = 'Hello World!';
console.log(mystr.valueOf()); // Hello World!
  1. trim()

trim() 方法会从一个字符串的两端删除空白字符。在这个上下文中的空白字符是所有的空白字符 (space, tab, no-break space 等) 以及所有行终止符字符(如 LF,CR)

let name = ' .ganshibiao. ';
console.log(name.trim());  // 'ganshibiao'

console.log(name.trimLeft());  // 'ganshibiao '
console.log(name.trimRight());  // ' ganshibiao'
console.log(name.trimStart());  // 'ganshibiao '
console.log(name.trimEnd());  // ' ganshibiao'