JavaScript string.includes() 方法
在编程语言中,字符串被认为是一系列字符。 JavaScript 字符串并不是从中产生的,但 JavaScript 所做的有趣的事情是自动将字符串转换为字符串对象。 因此,我们可以使用 (.) 符号轻松访问 String 的方法和属性。 string.includes() 方法就是其中之一。
string.includes() 的语法:
string.includes(targetString, indexPosition)
参数
- targetString 我们要搜索的特定字符串。 它是必需的。
- indexPosition 我们要开始搜索的特定索引位置。 默认从索引位置0开始,可选
返回值
来自字符串的布尔值如果字符串存在则返回真;否则返回真。 否则,假的。
重要提示
:string.includes() 方法区分大小写。 这意味着如果实际字符串是大写形式并且您从中搜索小写形式,它将不匹配。
使用具有必需参数 targetString 的 string.includes() 方法查找字符串
let str = 'Good things come to those who wait'
console.log(str.includes('come'))
console.log(str.includes('go'))
输出:
true
false
我们提供一个字符串来检查它是否存在于str中。 在这种情况下,str.includes() 方法找到字符串并返回 true
我们提供了另一个在 str 中不存在的 go 并返回 false
使用带有可选参数 indexPosition 的 string.includes() 方法查找字符串
let str = 'Look before you leap'
console.log(str.includes('before', 5))
console.log(str.includes('before', 10))
输出:
true
false
我们在索引位置 5 之前提供一个字符串。str.includes() 方法从索引位置 5 开始搜索目标字符串。给定的字符串之前从索引位置 5 开始并返回 true。
我们也检查相同的字符串,但这次我们提供索引位置 10。 结果,该方法找不到字符串并返回 false。
使用 string.includes() 方法查找区分大小写的字符串
let str = 'FACE THE MUSIC'
console.log(str.includes('FACE'))
console.log(str.includes('face'))
输出:
true
false
我们提供了一个大写形式的字符串 FACE,str.includes() 方法以准确的形式找到该字符串并返回 true。
我们提供相同的字符串面,但这次是小写形式。 即使字母相同,但形式与实际字符串不匹配,str.includes() 方法返回 false。
使用 string.includes() 方法在真实场景中查找字符串
let fruitStore = ['Apple', 'Orange', 'Mango', 'Cherry', 'Graps']
console.log(fruitStore.includes('Orange'))
console.log(fruitStore.includes('Banana'))
输出:
true
false
假设我们有一个销售不同水果的电子商务应用程序。 我们的程序中有一个字符串数组,用于存储所有水果的名称。 现在,我们要检查商店中是否有特定水果。 为此,我们使用了 str.includes()
方法。
我们提供一个字符串 Orange 来检查它是否存在于 fruitStroe 上。 includes()
方法找到水果并返回 true。
我们向 includes()
方法提供另一个字符串 Banana。 这次它没有找到任何名称为 Banana 的水果并返回 false。
相关文章
Do you understand JavaScript closures?
发布时间:2025/02/21 浏览次数:108 分类:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
发布时间:2025/02/21 浏览次数:178 分类:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
发布时间:2025/02/21 浏览次数:150 分类:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
用 jQuery 检查复选框是否被选中
发布时间:2024/03/24 浏览次数:102 分类:JavaScript
-
在本教程中学习 jQuery 检查复选框是否被选中的所有很酷的方法。我们展示了使用直接 DOM 操作、提取 JavaScript 属性的 jQuery 方法以及使用 jQuery 选择器的不同方法。你还将找到许多有用的
jQuery 中的 Window.onload 与 $(document).ready
发布时间:2024/03/24 浏览次数:180 分类:JavaScript
-
本教程演示了如何在 jQuery 中使用 Window.onload 和 $(document).ready 事件。