Advertisement
Guest User

Untitled

a guest
Jul 24th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.53 KB | None | 0 0
  1. String.prototype.contains = function(it) { return this.indexOf(it) != -1; };
  2.  
  3. indexof()
  4.  
  5. indexOf()
  6.  
  7. if (test.indexOf("title") !=-1) {
  8. alert(elm);
  9. foundLinks++;
  10. }
  11.  
  12. var index = haystack.indexOf(needle);
  13.  
  14. "potato".includes("to");
  15. > true
  16.  
  17. require('es6-shim')
  18.  
  19. var contained = str.includes(searchString [, position]);
  20.  
  21. searchString
  22.  
  23. position
  24.  
  25. var str = "To be, or not to be, that is the question.";
  26.  
  27. console.log(str.includes("To be")); // true
  28. console.log(str.includes("question")); // true
  29. console.log(str.includes("To be", 1)); // false
  30.  
  31. if (~haystack.indexOf('needle')) alert('found');
  32.  
  33. if (someStr.indexOf("a") >= 0) {
  34. // Found it
  35. } else {
  36. // Not Found
  37. }
  38.  
  39. if (~someStr.indexOf("a")) {
  40. // Found it
  41. } else {
  42. // Not Found
  43. }
  44.  
  45. var str="This is testing for javascript search !!!";
  46. if(str.search("for") != -1) {
  47. //logic
  48. }
  49.  
  50. if (!String.prototype.contains) {
  51. String.prototype.contains = function (arg) {
  52. return !!~this.indexOf(arg);
  53. };
  54. }
  55.  
  56. var s = "foo";
  57. alert(s.indexOf("oo") > -1);
  58.  
  59. var msg = "Hello world!";
  60.  
  61. console.log(msg.startsWith("Hello")); // true
  62. console.log(msg.endsWith("!")); // true
  63. console.log(msg.includes("o")); // true
  64.  
  65. console.log(msg.startsWith("o", 4)); // true
  66. console.log(msg.endsWith("o", 8)); // true
  67. console.log(msg.includes("o", 8)); // false
  68.  
  69. $("div:contains('John')")
  70.  
  71. if (eventString.indexOf("Deleted:") == -1)
  72.  
  73. var test = elm.getAttribute("className");
  74. //or
  75. var test = elm.className
  76.  
  77. _.includes('foobar', 'ob');
  78. // → true
  79.  
  80. _.str.include('foobar', 'ob');
  81. // → true
  82.  
  83. 'foobar'.includes('ob');
  84. // → true
  85.  
  86. x = "teststring";
  87.  
  88. if (x.match("test")) {
  89. // Code
  90. }
  91.  
  92. var allLinks = content.document.getElementsByTagName("a")
  93. , il = allLinks.length
  94. , i = 0
  95. , test
  96. , alrt;
  97.  
  98. while (i < il) {
  99. elm = allLinks[i++];
  100. test = elm.getAttribute("class");
  101.  
  102. if (test.indexOf("title") > -1)
  103. console.log(elm), foundLinks++;
  104. }
  105. alrt = foundLinks ? "Found " + foundLinks + " title class" : "No title class found";
  106. console.log(alrt);
  107.  
  108. if(~"John".indexOf("J")) {
  109. alert("Found")
  110. }
  111. else {
  112. alert("Not Found");
  113. }
  114.  
  115. var a = "Test String";
  116.  
  117. if(a.search("ring")!=-1){
  118. //exist
  119. } else {
  120. //not found
  121. }
  122.  
  123. if (!String.prototype.contains) {
  124. String.prototype.contains= function() {
  125. return String.prototype.indexOf.apply(this, arguments) !== -1;
  126. };
  127. }
  128.  
  129. "hello".contains("he") // true
  130. "hello world".contains("lo w")//true
  131. "hello world".contains("lo wa")//false
  132. "hello world".contains(" ")//true
  133. "hello world".contains(" ")//false
  134.  
  135. function stringContains(inputString, stringToFind) {
  136. return (inputString.indexOf(stringToFind) != -1);
  137. }
  138.  
  139. <html>
  140. <head>
  141. <h2>Use of contains() method</h2>
  142. <script>
  143. Array.prototype.contains = function (element) {
  144. for (var i = 0; i < this.length; i++) {
  145. if (this[i] == element) {
  146. return true;
  147. }
  148. }
  149. return false;
  150. }
  151. arr1 = ["Rose", "India", "Technologies"];
  152. document.write("The condition is "+arr1.contains("India")+"<br>");
  153. </script>
  154. </head>
  155.  
  156. <b>[If the specified element is present in the array, it returns true otherwise
  157. returns false.]</b>
  158.  
  159. </html>
  160.  
  161. var str = "My big string contain apples and oranges";
  162. var n = str.indexOf("apples");
  163. alert(n); //will alert 22, -1 if not found
  164.  
  165. <p>My big string contain apples and oranges</p>
  166. alert($("p:contains(apples)")[0] != undefined); //will alert true if found
  167.  
  168. var stringData ="anyString Data";
  169.  
  170. var subStringToSearch = "any";
  171.  
  172. // This will give back the substring if matches and if not returns null
  173. var doesContains = stringData.match(subStringToSearch);
  174.  
  175. if(doesContains !=null) {
  176. alert("Contains Substring");
  177. }
  178.  
  179. string = "asdf";
  180. substr = "as";
  181. alert(string.indexOf(substr) == -1 ? false : true);
  182.  
  183. String.prototype.contains = function(test) {
  184. return this.indexOf(test) == -1 ? false : true;
  185. };
  186.  
  187. string = "asdf";
  188. alert(string.contains("as"));
  189.  
  190. if (!String.prototype.contains) {
  191. String.prototype.contains = function (arg) {
  192. return !!~this.indexOf(arg);
  193. };
  194. }
  195.  
  196. 'Hello World'.contains('orl');
  197.  
  198. var helper = {};
  199.  
  200. helper.string = {
  201. contains : function (haystack, needle) {
  202. return !!~haystack.indexOf(needle);
  203. },
  204. ...
  205. };
  206.  
  207. helper.string.contains('Hello World', 'orl');
  208.  
  209. _.includes('Hello World', 'orl');
  210.  
  211. _.str.include('Hello World', 'orl');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement