盘点下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,一起交流面经,一起屡败屡战。

相关推荐
夏幻灵16 分钟前
HTML5里最常用的十大标签
前端·html·html5
Mr Xu_30 分钟前
Vue 3 中 watch 的使用详解:监听响应式数据变化的利器
前端·javascript·vue.js
未来龙皇小蓝33 分钟前
RBAC前端架构-01:项目初始化
前端·架构
程序员agions41 分钟前
2026年,微前端终于“死“了
前端·状态模式
万岳科技系统开发42 分钟前
食堂采购系统源码库存扣减算法与并发控制实现详解
java·前端·数据库·算法
程序员猫哥_1 小时前
HTML 生成网页工具推荐:从手写代码到 AI 自动生成网页的进化路径
前端·人工智能·html
龙飞051 小时前
Systemd -systemctl - journalctl 速查表:服务管理 + 日志排障
linux·运维·前端·chrome·systemctl·journalctl
我爱加班、、1 小时前
Websocket能携带token过去后端吗
前端·后端·websocket
AAA阿giao1 小时前
从零拆解一个 React + TypeScript 的 TodoList:模块化、数据流与工程实践
前端·react.js·ui·typescript·前端框架
杨超越luckly1 小时前
HTML应用指南:利用GET请求获取中国500强企业名单,揭秘企业增长、分化与转型的新常态
前端·数据库·html·可视化·中国500强