Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- String.prototype.contains = function(it) { return this.indexOf(it) != -1; };
- indexof()
- indexOf()
- if (test.indexOf("title") !=-1) {
- alert(elm);
- foundLinks++;
- }
- var index = haystack.indexOf(needle);
- "potato".includes("to");
- > true
- require('es6-shim')
- var contained = str.includes(searchString [, position]);
- searchString
- position
- var str = "To be, or not to be, that is the question.";
- console.log(str.includes("To be")); // true
- console.log(str.includes("question")); // true
- console.log(str.includes("To be", 1)); // false
- if (~haystack.indexOf('needle')) alert('found');
- if (someStr.indexOf("a") >= 0) {
- // Found it
- } else {
- // Not Found
- }
- if (~someStr.indexOf("a")) {
- // Found it
- } else {
- // Not Found
- }
- var str="This is testing for javascript search !!!";
- if(str.search("for") != -1) {
- //logic
- }
- if (!String.prototype.contains) {
- String.prototype.contains = function (arg) {
- return !!~this.indexOf(arg);
- };
- }
- var s = "foo";
- alert(s.indexOf("oo") > -1);
- var msg = "Hello world!";
- console.log(msg.startsWith("Hello")); // true
- console.log(msg.endsWith("!")); // true
- console.log(msg.includes("o")); // true
- console.log(msg.startsWith("o", 4)); // true
- console.log(msg.endsWith("o", 8)); // true
- console.log(msg.includes("o", 8)); // false
- $("div:contains('John')")
- if (eventString.indexOf("Deleted:") == -1)
- var test = elm.getAttribute("className");
- //or
- var test = elm.className
- _.includes('foobar', 'ob');
- // → true
- _.str.include('foobar', 'ob');
- // → true
- 'foobar'.includes('ob');
- // → true
- x = "teststring";
- if (x.match("test")) {
- // Code
- }
- var allLinks = content.document.getElementsByTagName("a")
- , il = allLinks.length
- , i = 0
- , test
- , alrt;
- while (i < il) {
- elm = allLinks[i++];
- test = elm.getAttribute("class");
- if (test.indexOf("title") > -1)
- console.log(elm), foundLinks++;
- }
- alrt = foundLinks ? "Found " + foundLinks + " title class" : "No title class found";
- console.log(alrt);
- if(~"John".indexOf("J")) {
- alert("Found")
- }
- else {
- alert("Not Found");
- }
- var a = "Test String";
- if(a.search("ring")!=-1){
- //exist
- } else {
- //not found
- }
- if (!String.prototype.contains) {
- String.prototype.contains= function() {
- return String.prototype.indexOf.apply(this, arguments) !== -1;
- };
- }
- "hello".contains("he") // true
- "hello world".contains("lo w")//true
- "hello world".contains("lo wa")//false
- "hello world".contains(" ")//true
- "hello world".contains(" ")//false
- function stringContains(inputString, stringToFind) {
- return (inputString.indexOf(stringToFind) != -1);
- }
- <html>
- <head>
- <h2>Use of contains() method</h2>
- <script>
- Array.prototype.contains = function (element) {
- for (var i = 0; i < this.length; i++) {
- if (this[i] == element) {
- return true;
- }
- }
- return false;
- }
- arr1 = ["Rose", "India", "Technologies"];
- document.write("The condition is "+arr1.contains("India")+"<br>");
- </script>
- </head>
- <b>[If the specified element is present in the array, it returns true otherwise
- returns false.]</b>
- </html>
- var str = "My big string contain apples and oranges";
- var n = str.indexOf("apples");
- alert(n); //will alert 22, -1 if not found
- <p>My big string contain apples and oranges</p>
- alert($("p:contains(apples)")[0] != undefined); //will alert true if found
- var stringData ="anyString Data";
- var subStringToSearch = "any";
- // This will give back the substring if matches and if not returns null
- var doesContains = stringData.match(subStringToSearch);
- if(doesContains !=null) {
- alert("Contains Substring");
- }
- string = "asdf";
- substr = "as";
- alert(string.indexOf(substr) == -1 ? false : true);
- String.prototype.contains = function(test) {
- return this.indexOf(test) == -1 ? false : true;
- };
- string = "asdf";
- alert(string.contains("as"));
- if (!String.prototype.contains) {
- String.prototype.contains = function (arg) {
- return !!~this.indexOf(arg);
- };
- }
- 'Hello World'.contains('orl');
- var helper = {};
- helper.string = {
- contains : function (haystack, needle) {
- return !!~haystack.indexOf(needle);
- },
- ...
- };
- helper.string.contains('Hello World', 'orl');
- _.includes('Hello World', 'orl');
- _.str.include('Hello World', 'orl');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement