• 【JavaScript】String 实例方法(一)


    以下内容为学习记录,可以参考 MDN 原文。

    环境

    • node v12.18.1
    • npm 6.14.5
    • vscode 1.46
    • Microsoft Edge 83

    charAt

    charAt() 方法从一个字符串中返回指定的字符。

    const sentence = 'The quick brown fox jumps over the lazy dog.';
    
    const index = 4;
    
    console.log(`The character at index ${index} is ${sentence.charAt(index)}`);
    // expected output: "The character at index 4 is q"
    

    charCodeAt

    charCodeAt() 方法返回 0 到 65535 之间的整数,表示给定索引处的 UTF-16 代码单元 。如果你想要整个代码点的值,使用 codePointAt()。

    const sentence = 'The quick brown fox jumps over the lazy dog.';
    
    const index = 4;
    
    console.log(`The character code ${sentence.charCodeAt(index)} is equal to ${sentence.charAt(index)}`);
    // expected output: "The character code 113 is equal to q"
    

    codePointAt

    codePointAt() 方法返回 一个 Unicode 编码点值的非负整数。

    const icons = '☃★♲';
    
    console.log(icons.codePointAt(1));
    // expected output: "9733"
    

    concat

    concat() 方法将一个或多个字符串与原字符串连接合并,形成一个新的字符串并返回。

    强烈建议使用 赋值操作符(+, +=)代替 concat 方法。

    const str1 = 'Hello';
    const str2 = 'World';
    
    console.log(str1.concat(' ', str2));
    // expected output: "Hello World"
    
    console.log(str2.concat(', ', str1));
    // expected output: "World, Hello"
    

    includes

    includes() 方法用于判断一个字符串是否包含在另一个字符串中,根据情况返回 true 或 false。

    const sentence = 'The quick brown fox jumps over the lazy dog.';
    
    const word = 'fox';
    
    console.log(`The word "${word}" ${sentence.includes(word) ? 'is' : 'is not'} in the sentence`);
    // expected output: "The word "fox" is in the sentence"
    

    endsWith

    endsWith() 方法用来判断当前字符串是否是以另外一个给定的子字符串结尾的,根据判断结果返回 true 或 false。

    const str1 = 'Cats are the best!';
    
    console.log(str1.endsWith('best', 17));
    // expected output: true
    
    const str2 = 'Is this a question';
    
    console.log(str2.endsWith('?'));
    // expected output: false
    

    indexOf

    indexOf() 方法返回调用它的 String 对象中第一次出现的指定值的索引,从 fromIndex 处进行搜索。如果未找到该值,则返回 -1。

    const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';
    
    const searchTerm = 'dog';
    const indexOfFirst = paragraph.indexOf(searchTerm);
    
    console.log(`The index of the first "${searchTerm}" from the beginning is ${indexOfFirst}`);
    // expected output: "The index of the first "dog" from the beginning is 40"
    
    console.log(`The index of the 2nd "${searchTerm}" is ${paragraph.indexOf(searchTerm, (indexOfFirst + 1))}`);
    // expected output: "The index of the 2nd "dog" is 52"
    

    lastIndexOf

    lastIndexOf() 方法返回调用 String 对象的指定值最后一次出现的索引,在一个字符串中的指定位置 fromIndex 处从后向前搜索。如果没找到这个特定值则返回-1 。

    const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';
    
    const searchTerm = 'dog';
    
    console.log(`The index of the first "${searchTerm}" from the end is ${paragraph.lastIndexOf(searchTerm)}`);
    // expected output: "The index of the first "dog" from the end is 52"
    

    localeCompare

    localeCompare() 方法返回一个数字来指示一个参考字符串是否在排序顺序前面或之后或与给定字符串相同。

    const a = 'réservé'; // with accents, lowercase
    const b = 'RESERVE'; // no accents, uppercase
    
    console.log(a.localeCompare(b));
    // expected output: 1
    console.log(a.localeCompare(b, 'en', { sensitivity: 'base' }));
    // expected output: 0
    

    match

    match() 方法检索返回一个字符串匹配正则表达式的的结果。

    const paragraph = 'The quick brown fox jumps over the lazy dog. It barked.';
    const regex = /[A-Z]/g;
    const found = paragraph.match(regex);
    
    console.log(found);
    // expected output: Array ["T", "I"]
    

    matchAll

    matchAll() 方法返回所有与正则表达式匹配字符串的结果的迭代器,包括捕获组。

    let regexp = /t(e)(st(d?))/g;
    let str = 'test1test2';
    
    let array = [...str.matchAll(regexp)];
    
    console.log(array[0]);
    // expected output: Array ["test1", "e", "st1", "1"]
    
    console.log(array[1]);
    // expected output: Array ["test2", "e", "st2", "2"]
    
  • 相关阅读:
    爬取校园新闻首页的新闻
    网络爬虫基础练习
    综合练习:词频统计
    免费的论文查重网站
    Hadoop综合大作业
    理解MapReduce
    熟悉常用的HBase操作
    熟悉常用的HDFS操作
    爬虫大作业
    数据结构化与保存
  • 原文地址:https://www.cnblogs.com/jiangbo44/p/13574284.html
Copyright © 2020-2023  润新知