10 个很棒的 JavaScript 字符串技巧

3b0b9ffb7529f0e68c5c2d6f46290dda.png

英文 | https://javascript.plainenglish.io/10-great-javascript-string-tips-55185e4d6b3

翻译 | 杨小爱

字符串是几乎所有编程语言中的基本类型之一。以下10 个重要的JS技巧可能是你不知道的。

那么,我们现在就开始吧。

1.如何多次复制一个字符串

JS 字符串允许简单的重复,不同于纯手工复制字符串,我们可以使用字符串重复的方法。

const laughing = 'Maxwell '.repeat(3)
consol.log(laughing) // "Maxwell Maxwell Maxwell "


const eightBits = '1'.repeat(8)
console.log(eightBits) // "11111111"

2.如何将字符串填充到指定长度

有时我们希望字符串具有特定的长度。如果字符串太短,则需要填充剩余空间,直到达到指定长度。

以前主要用库left-pad。但是,今天我们可以使用 padStart 和 SpadEnd 方法,选择取决于字符串是在字符串的开头还是结尾填充。

// Add "0" to the beginning until the length of the string is 8.
const eightBits = '001'.padStart(8, '0')
console.log(eightBits) // "00000001"


//Add " *" at the end until the length of the string is 5.
const anonymizedCode = "34".padEnd(5, "*")
console.log(anonymizedCode) // "34***"

3.如何将一个字符串分割成一个字符数组

有几种方法可以将字符串拆分为字符数组,我更喜欢使用扩展运算符 (...) :

const word = 'Maxwell'
 const characters = [...word]
 console.log(characters)

4.如何计算字符串中的字符

可以使用长度属性。

const word = "apple";
console.log(word.length) // 5

5.如何反转字符串中的字符

反转字符串中的字符很容易,只需组合扩展运算符 (...)、Array.reverse 方法和 Array.join 方法。

const word = "apple"
const reversedWord = [...word].reverse().join("")
console.log(reversedWord) // "elppa"

6.如何将字符串中的第一个字母大写

一个非常常见的操作是将字符串的首字母大写,虽然许多编程语言都有一种原生的方式来做到这一点,但 JS 需要做一些工作。

let word = 'apply'


word = word[0].toUpperCase() + word.substr(1)


console.log(word) // "Apple"

另一种方法:

// This shows an alternative way
let word = "apple";




const characters = [...word];
characters[0] = characters[0].toUpperCase();
word = characters.join("");


console.log(word); // "Apple"

7.如何在多个分隔符上拆分字符串

假设我们要在一个分隔符上拆分一个字符串,我们首先想到的就是使用split方法,这个方法当然是聪明人都知道的。但是,你可能不知道的一点是,split 可以同时拆分多个定界符,这可以通过使用正则表达式来实现。

const list = "apples,bananas;cherries"
const fruits = list.split(/[,;]/)
console.log(fruits); // ["apples", "bananas", "cherries"]

8. 如何检查字符串是否包含特定序列

字符串搜索是一项常见任务,在 JS 中,你可以使用 String.includes 方法轻松完成此操作,不需要正则表达式。

const text = "Hello, world! My name is Kai!"
console.log(text.includes("Kai")); // true

9. 如何检查字符串是否以特定序列开始或结束

要在字符串的开头或结尾搜索,可以使用 String.startsWith 和 String.endsWith 方法。

const text = "Hello, world! My name is Kai!"


console.log(text.startsWith("Hello")); // true


console.log(text.endsWith("world")); // false

10.如何替换所有出现的字符串

有多种方法可以替换所有出现的字符串,您可以使用 String.replace 方法和带有全局标志的正则表达式;或者使用新的 String.replaceAll 方法,请注意,此新方法并非在所有浏览器和 Node.js 版本中都可用。

const text = "I like apples. You like apples."


console.log(text.replace(/apples/g, "bananas"));
// "I like bananas. You like bananas."


console.log(text.replaceAll("apples", "bananas"));

总结

字符串是几乎所有编程语言中最基本的数据类型之一。此外,它是新开发人员最先学习的数据类型之一。然而,尤其是在 JavaScript 中,许多开发人员并不知道有关字符串的一些有趣细节,我们希望这篇文章对您有所帮助,如果你觉得有用,请记得点赞我,关注我。

感谢你的阅读,快乐学习,开心编程。

学习更多技能

请点击下方公众号

45a46b9fd38f0ea1220a479ad82b133e.gif

965db9be8d387976d295161cd58d04f6.jpeg

85c731403f2d564c098267ad301d4fd3.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值