js 好用的字符操作方法

序:突然感觉有些方法常见有时也用,但怕有时不记得,顺便记录一下!

一、获取类方法

javascript 复制代码
let str = "Hello,你们好!"
console.log(str.charAt(6))       // 你
console.log(str.charAt(12))      // (空字符串)

console.log(str.charCodeAt(2))   // 108
console.log(str.charCodeAt(12))  // NaN

console.log(String.fromCharCode(97,98,99,100))  // abcd

1)charAt()

charAt()方法可用来获取指定位置的字符串,index为字符串索引值,index的范围从0开始到string.length--1,若不在这个范围将返回一个空字符串。

2)charCodeAt()

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

3)fromCharCode()

fromCharCode()可接受一个或多个Unicode值,然后返回一个字符串。

二。查找类方法

javascript 复制代码
let str = "Hello,你们好!"
console.log(str.indexOf("l"))     // 2
console.log(str.indexOf("l",3))   // 3
console.log(str.indexOf("f"))     // -1

console.log(str.lastIndexOf("l")) // 3
console.log(str.lastIndexOf("f")) // -1

console.log(str.search("l"))      // 2
console.log(str.search("f"))      // -1
console.log(str.search(/Llo/i))   // 2

console.log(str.match("你们好"))  // ["你们好", index: 6, input: "Hello,你们好!", groups: undefined]
console.log(str.match(/\w/))      // ["H", index: 0, input: "Hello,你们好!", groups: undefined]
console.log(str.match("哈哈"))    // null

1)indexOf()

indexOf()用来检索指定的字符串值在字符串中首次出现的位置。

2)lastIndexOf()

lastIndexOf()语法与indexOf()类似,它返回的是一个指定的子字符串值最后出现的位置,其检索顺序是从后向前,没有找到则返回-1。

3)search()

search()方法用于检索字符串中指定的子字符串,或检索与正则表达式相匹配的子字符串。它会返回第一个匹配的子字符串的起始位置,如果没有匹配的,则返回-1。

4)match()

match()方法可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配。

三、截取类方法

javascript 复制代码
let str = "Hello,你们好!"
console.log(str.substring(2,5))  // llo
console.log(str.substring(2))    // llo,你们好!

console.log(str.substr(2,5))     // llo,你
console.log(str.substr(-2,3))    // 好!

console.log(str.slice(2,5))      // llo
console.log(str.slice(-5,-2))    // ,你们

1)substring()

substring()是最常用到的字符串截取方法,它可以接收两个参数(参数不能为负值 ),分别是要截取的开始位置和结束位置,它将返回一个新的字符串,其内容是从start处到end-1处的所有字符。

2)substr()

substr()方法可在字符串中抽取从start下标开始的指定数目的字符。

3)slice()

slice()方法与substring()方法非常类似,它传入的两个参数也分别对应着开始位置和结束位置。

四、其他字符串方法

javascript 复制代码
let str = "Hello,你们好!"
console.log(str.replace("你们","大家")) // Hello,大家好!
console.log(str.replace(/\w/,"*"))     // *ello,你们好!
console.log(str.replace(/\w/g,"*"))    // *****,你们好!

console.log(str.split(","))            // ["Hello", "你们好!"]

console.log(str.toLowerCase())         // hello,你们好!
console.log(str.toUpperCase())         // HELLO,你们好!

console.log(str.concat("去哪!"))       // Hello,你们好!去哪!

1)replace()

replace()方法用来进行字符串替换操作,它可以接收两个参数,前者为被替换的子字符串(可以是正则表达式),后者为用来替换的文本。

2)split()

split()方法用于把一个字符串分割成字符串数组。

3)toLowerCase() 和 toUpperCase()

toLowerCase()方法可以把字符串中的大写字母转换为小写,toUpperCase()方法可以把字符串中的小写字母转换为大写。

4)concat()

concat() 方法用于连接两个或多个字符串,相当于"+"运算符。

5)padStart() 和 padEnd()

复制代码
str.padStart(targetLength, padString) 
  • targetLength:目标字符串的长度。
  • padString:用于填充的字符串。如果省略,默认使用空格填充。
javascript 复制代码
const str = "hello";
const t1 = str.padStart(10, false);// 结果:'falsehello'
const t2 = str.padStart(10, null); // 结果:'nullnhello'
const t3 = str.padStart(10, []);   // 结果:'hello',因为[]转换成字符串是空字符串
const t4 = str.padStart(10, {});   // 结果:'[objehello'
console.log("打印",t1,t2,t3,t4);
const t21 = str.padEnd(10, false);// 结果:'hellofalse'
const t22 = str.padEnd(10, null); // 结果:'hellonulln'
const t23 = str.padEnd(10, []);   // 结果:'hello',因为[]转换成字符串是空字符串
const t24 = str.padEnd(10, {});   // 结果:'hello[obje'
console.log("打印2",t21,t22,t23,t24);

padEnd() 顾名思义与 padStart() 相反,padStart() 填充的在前,padEnd() 填充的在后;

6)trim()、trimStart() 和 trimEnd()

trimLeft() 方法是trimStart() 方法的别名。 trimLeft() 具有与 trimLeft() 方法相同的功能。 建议您使用 trimStart() 方法。

trimRight() 方法是trimEnd() 方法的别名。 trimRight() 提供与 trimRight() 方法相同的功能。 但是,建议您使用 trimEnd() 方法。

javascript 复制代码
const str = "   hello world   ";
const start = str.trimStart();
const left = str.trimLeft();
const center = str.trim();
const end = str.trimEnd();
const right = str.trimRight();
console.log("打印",{start,left,center,end,right})
// 打印 {start: "hello world  ", left: "hello world  ", center: "hello world", end: "   hello world", right: "   hello world"}
相关推荐
Yannik19 分钟前
在 Apple Silicon Mac 上用 Jenkins + Fastlane 搭建 iOS CI:从自动打包到 MR 检查流水线的完整实践
前端
Nayana19 分钟前
《Web 到 HarmonyOS》-- 打包:Vite 产物 → HAP / HAR / HSP
前端
inksci26 分钟前
用飞帆做了个小网页
前端·javascript·css·html
去伪存真30 分钟前
被 pnpm 打包坑惨后,我彻底盘明白了硬链接与软链接
前端·electron
zhensherlock1 小时前
Protocol Launcher 系列:CodeHub 移动端 GitHub 协作利器
前端·javascript·typescript·node.js·自动化·github·js
多加点辣也没关系4 小时前
JavaScript|第40章:JavaScript 工程化实践
开发语言·javascript·ecmascript
彩虹暴龙兽6 小时前
JavaScript 预编译机制与执行上下文构建分析
javascript
tryCbest6 小时前
Vue3——第 22 篇:Pinia——新一代状态管理(上)
前端·javascript·vue.js
bai2737 小时前
"👨‍👩‍👧‍👦" 的 length 是 11:我把自己写的字数统计脚本拖出来审了一遍
前端·javascript
bai2737 小时前
同一个 1.005,toFixed 给 1.00、Math.round 给 1、Intl 给 1.01:999 个三分位小数的实测
javascript·后端