Lodash _.strContains() 方法
Lodash _.strContains() 方法检查给定字符串是否包含搜索到的字符串,并返回相应的布尔值。
句法:
_.strContains( string, searched_str);
参数:此方法接受上面提到的两个参数,如下所述:
- String:此方法接受要在其中搜索搜索字符串的字符串。
- Searched_str:此方法接受要搜索的字符串。
返回值:此方法返回一个布尔值。
注意:这在普通 JavaScript 中不起作用,因为它需要安装 lodash.js contrib 库。 Lodash.js contrib 库可以使用npm install lodash-contrib –save 安装。
示例 1:
Javascript
// Defining lodash contrib variable
var _ = require('lodash-contrib');
var bool = _.strContains("GeeksforGeeks", "Geeks");
console.log("The String contains the "
+ "searched String : ", bool);
Javascript
// Defining lodash contrib variable
var _ = require('lodash-contrib');
var bool = _.strContains("abc", "d");
console.log("The String contains the "
+ "searched String : ", bool);
Javascript
// Defining lodash contrib variable
var _ = require('lodash-contrib');
var bool = _.strContains("abc", "a");
console.log("The String contains the "
+ "searched String : ", bool);
输出:
The String contains the searched String : true
示例 2:
Javascript
// Defining lodash contrib variable
var _ = require('lodash-contrib');
var bool = _.strContains("abc", "d");
console.log("The String contains the "
+ "searched String : ", bool);
输出:
The String contains the searched String : false
示例 3:
Javascript
// Defining lodash contrib variable
var _ = require('lodash-contrib');
var bool = _.strContains("abc", "a");
console.log("The String contains the "
+ "searched String : ", bool);
输出:
The String contains the searched String : true