首页 > web前端 > js教程 > 正文

最常见的 JavaScript 字符串方法列表

Linda Hamilton
发布: 2024-11-19 19:22:03
原创
639 人浏览过

list of the most common JavaScript string methods

最常见的 JavaScript 字符串方法列表:

  • 长度:
let message = "Hello, world!";
console.log(message.length); // Output: 13
登录后复制
  • concat():
let greeting = "Hello";
let name = "Alice";
let fullGreeting = greeting.concat(", ", name, "!");
console.log(fullGreeting); // Output: "Hello, Alice!"
登录后复制
  • charAt():
let message = "JavaScript";
console.log(message.charAt(0)); // Output: "J"
console.log(message.charAt(5)); // Output: "S"
登录后复制
  • indexOf():
let sentence = "The quick brown fox jumps over the lazy dog.";
console.log(sentence.indexOf("quick")); // Output: 4
console.log(sentence.indexOf("dog")); // Output: 37
console.log(sentence.indexOf("lion")); // Output: -1 (not found)
登录后复制
  • lastIndexOf():
let quote = "To be, or not to be, that is the question.";
console.log(quote.lastIndexOf("be")); // Output: 23
console.log(quote.lastIndexOf("the")); // Output: 33
登录后复制
  • 切片():
let fruit = "apple, banana, cherry";
console.log(fruit.slice(0, 5)); // Output: "apple"
console.log(fruit.slice(7, 13)); // Output: "banana"
console.log(fruit.slice(15)); // Output: "cherry"
登录后复制
  • 子字符串():
let text = "Hello, world!";
console.log(text.substring(0, 5)); // Output: "Hello"
console.log(text.substring(7, 12)); // Output: "world"
登录后复制
  • substr():
let message = "The quick brown fox jumps.";
console.log(message.substr(4, 5)); // Output: "quick"
console.log(message.substr(10)); // Output: "brown fox jumps."
登录后复制
  • toLowerCase():
let text = "JAVASCRIPT IS AWESOME!";
console.log(text.toLowerCase()); // Output: "javascript is awesome!"
登录后复制
  • toUpperCase():
let text = "hello, world!";
console.log(text.toUpperCase()); // Output: "HELLO, WORLD!"
登录后复制
  • 修剪():
let greeting = "   Hello, world!   ";
console.log(greeting.trim()); // Output: "Hello, world!"
登录后复制
  • 替换():
let text = "I like cats. Cats are fluffy.";
console.log(text.replace("cats", "dogs")); // Output: "I like dogs. Cats are fluffy."
console.log(text.replace(/cats/i, "dogs")); // Output: "I like dogs. Cats are fluffy." (case-insensitive)
登录后复制
  • 分割():
let csv = "apple,banana,cherry";
let fruits = csv.split(",");
console.log(fruits); // Output: ["apple", "banana", "cherry"]
登录后复制
  • 包括():
let message = "The quick brown fox jumps over the lazy dog.";
console.log(message.includes("quick")); // Output: true
console.log(message.includes("tiger")); // Output: false
登录后复制
  • startsWith():
let greeting = "Hello, world!";
console.log(greeting.startsWith("Hello")); // Output: true
console.log(greeting.startsWith("world")); // Output: false
登录后复制
  • endsWith():
let filename = "myfile.pdf";
console.log(filename.endsWith(".pdf")); // Output: true
console.log(filename.endsWith(".txt")); // Output: false
登录后复制
  • 匹配():
let text = "The quick brown fox jumps.";
let pattern = /quick/;
console.log(text.match(pattern)); // Output: ["quick"]
登录后复制
  • 搜索():
let text = "The quick brown fox jumps.";
let pattern = /brown/;
console.log(text.search(pattern)); // Output: 10
登录后复制
  • 重复():
let message = "Hello ";
console.log(message.repeat(3)); // Output: "Hello Hello Hello "
登录后复制
  • trimStart():
let greeting = "   Hello, world!   ";
console.log(greeting.trimStart()); // Output: "Hello, world!   "
登录后复制
  • trimEnd():
let greeting = "   Hello, world!   ";
console.log(greeting.trimEnd()); // Output: "   Hello, world!"
登录后复制
  • padStart():
let number = "42";
console.log(number.padStart(4, "0")); // Output: "0042"
登录后复制
  • padEnd():
    让 word = "JavaScript";
    console.log(word.padEnd(15, ".")); // 输出:“JavaScript.....”

  • 拼接():

let str = "Hello, world!";
let result = str.splice(7, 5, "everyone");
console.log(result); // Output: "world"
console.log(str); // Output: "Hello, everyone!"
登录后复制
  • toString():
let num = 42;
console.log(num.toString()); // Output: "42"
登录后复制
  • valueOf():
let str = "JavaScript";
console.log(str.valueOf()); // Output: "JavaScript"
登录后复制
  • charCodeAt():
let message = "Hello";
console.log(message.charCodeAt(0)); // Output: 72
console.log(message.charCodeAt(4)); // Output: 111
登录后复制
  • codePointAt():
let emoji = "?";
console.log(emoji.codePointAt(0)); // Output: 128512
登录后复制

标准化():

let str1 = "café";
let str2 = "cafe\u0301";
console.log(str1.normalize() === str2.normalize()); // Output: true
登录后复制

这涵盖了 JavaScript 中的各种字符串操作任务,从获取长度或连接字符串等基本操作到模式匹配和 Unicode 规范化等更高级的技术。

以上是最常见的 JavaScript 字符串方法列表的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板