盘点下JS字符串的常见方法,欢迎补充

一、增

  • concat
  • padEnd
  • padStart

concat()

concat()方法用于将两个或多个字符串连接在一起,并返回一个新的字符串。

js 复制代码
const str1 = 'Hello';
const str2 = 'World';
const result = str1.concat(' ', str2);
console.log(result); // Output: "Hello World"

padEnd() 和 padStart()

padEnd()padStart() 方法分别用于在字符串的末尾或开头填充指定的字符,直到字符串达到指定的长度。

js 复制代码
const str = 'Hello';
console.log(str.padEnd(10, '.')); // Output: "Hello....."
console.log(str.padStart(10, '.')); // Output: ".....Hello"

二、删

  • slice
  • substring
  • substr

slice()

slice() 方法从原始字符串中提取出指定位置范围的子字符串,并返回一个新的字符串,不会修改原始字符串。

js 复制代码
const str = 'Hello World';
console.log(str.slice(6)); // Output: "World"
console.log(str.slice(0, 5)); // Output: "Hello"

substring()

substring() 方法与 slice() 方法类似,但不支持负索引。它返回位于两个指定索引之间的子字符串。

js 复制代码
const str = 'Hello World';
console.log(str.substring(6)); // Output: "World"
console.log(str.substring(0, 5)); // Output: "Hello"

substr()

substr() 方法从指定位置开始提取指定长度的子字符串,并返回一个新的字符串。

js 复制代码
const str = 'Hello World';
console.log(str.substr(6)); // Output: "World"
console.log(str.substr(0, 5)); // Output: "Hello"

三、改

  • replace
  • repeat
  • trim
    • trimLeft
    • trimRight
  • toLowerCase
  • toUpperCase

replace()

replace() 方法用新字符串替换原始字符串中的匹配项。

js 复制代码
const str = 'Hello World';
console.log(str.replace('World', 'Universe')); // Output: "Hello Universe"

repeat()

repeat() 方法将字符串重复指定次数,并返回一个新的字符串。

js 复制代码
const str = 'Hello';
console.log(str.repeat(3)); // Output: "HelloHelloHello"

trim()

trim() 方法去除字符串两端的空白字符,并返回一个新的字符串。

js 复制代码
const str = '  Hello World  ';
console.log(str.trim()); // Output: "Hello World"

toLowerCase() 和 toUpperCase()

toLowerCase()toUpperCase() 方法分别将字符串转换为小写和大写形式。

js 复制代码
const str = 'Hello World';
console.log(str.toLowerCase()); // Output: "hello world"
console.log(str.toUpperCase()); // Output: "HELLO WORLD"

四、查

  • includes
  • indexOf
  • lastIndexOf
  • chatAt()
  • startsWith
  • endsWith

includes()

includes() 方法检查字符串是否包含指定的子字符串,并返回 true 或 false。

js 复制代码
const str = 'Hello World';
console.log(str.includes('World')); // Output: true
console.log(str.includes('Universe')); // Output: false

indexOf() 和 lastIndexOf()

indexOf()lastIndexOf() 方法分别返回字符串中指定子字符串的第一个匹配项和最后一个匹配项的索引。

js 复制代码
const str = 'Hello World';
console.log(str.indexOf('o')); // Output: 4
console.log(str.lastIndexOf('o')); // Output: 7

charAt()

charAt() 方法返回指定位置的字符。

js 复制代码
const str = 'Hello World';
console.log(str.charAt(6)); // Output: "W"

startsWith() 和 endsWith()

startsWith()endsWith() 方法分别检查字符串是否以指定的子字符串开头或结尾,并返回 true 或 false。

js 复制代码
const str = 'Hello World';
console.log(str.startsWith('Hello')); // Output: true
console.log(str.endsWith('World')); // Output: true

五、转换方法

  • charCodeAt
  • split

charCodeAt()

charCodeAt() 方法返回指定位置字符的 Unicode 编码。

js 复制代码
const str = 'Hello';
console.log(str.charCodeAt(0)); // Output: 72

split()

split() 方法将字符串分割为子字符串数组,使用指定的分隔符进行分割。

js 复制代码
const str = 'apple,banana,grape';
console.log(str.split(',')); // Output: ["apple", "banana", "grape"]

结语

如果这篇文章对你有帮助的话,欢迎点个赞收藏一下,你的点赞是对作者最好的激励,最后如果您也和我一样准备春招,,欢迎加我微信lx3122178991,一起交流面经,一起屡败屡战。

相关推荐
我是天龙_绍10 分钟前
浏览器指纹,一个挺实用的知识点
前端
theshy11 分钟前
前端自制接口抓取工具:一键收集并导出接口列表
前端
wayne21414 分钟前
跨平台开发框架全景分析:Flutter、RN、KMM 与腾讯 Kuikly 谁更值得选择?
前端
LuckySusu14 分钟前
【js篇】JavaScript 对象创建的 6 种方式:从基础到高级
前端·javascript
LuckySusu15 分钟前
【js篇】async/await 的五大核心优势:让异步代码像同步一样清晰
前端·javascript
艾雅法拉拉16 分钟前
JS知识点回顾(1)
前端·javascript·面试
LuckySusu21 分钟前
【js篇】Promise 解决了什么问题?—— 彻底告别“回调地狱”
前端·javascript
程序员海军27 分钟前
如何让AI真正理解你的需求
前端·后端·aigc
passer98131 分钟前
基于Vue的场景解决
前端·vue.js
yinke小琪32 分钟前
说说Java 中 Object 类的常用的几个方法?详细的讲解一下
java·后端·面试