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