Home >Web Front-end >JS Tutorial >JavaScript method to determine whether a string contains a specified substring_javascript skills

JavaScript method to determine whether a string contains a specified substring_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:08:421288browse

The example in this article describes how JavaScript determines whether a string contains a specified substring. Share it with everyone for your reference. The specific analysis is as follows:

The following JS code defines a contains method for the String object to determine whether the string contains substrings, which is very useful.

if (!Array.prototype.indexOf) {
  Array.prototype.indexOf = function(obj, start) {
     for (var i = (start || 0), j = this.length; i < j; i++) {
       if (this[i] === obj) { return i; }
     }
     return -1;
  }
}
if (!String.prototype.contains) {
  String.prototype.contains = function (arg) {
    return !!~this.indexOf(arg);
  };
}

The following is a detailed usage example, which can be executed in the browser

Copy code The code is as follows:
Enter two strings and check if Strign 1 contains String 2.0c6dc11e160d3b678d68754cc175188a 2a87851f231f0546ed00aa1a4409038e
String 1: 8b2dfcb1ad9bf3ba83dfe88c90df09d4  0c6dc11e160d3b678d68754cc175188a
String 2: 44d73486c1f5551b0081888bd7d6fb4b 0c6dc11e160d3b678d68754cc175188a0c6dc11e160d3b678d68754cc175188a
ad300d42e070861449b92412a058adf9Click to check if String 1 contains String 265281c5ac262bf6d81768915a4a77ac0
3f1c4e4b6b16bbbd69b2ee476dc4f83a
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function(obj, start) {
for (var i = (start || 0), j = this.length; i 260284935f702d38c2d353c4811ea552

I hope this article will be helpful to everyone’s JavaScript programming design.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn