How would our group find out if a particular string contain the scertain substring? Only with the help of Jquery, please.
+8
A:
You don't really need jQuery for such a simple thing, you can simply use the indexOf method of String objects, e.g.:
var str = "foobar";
var containsFoo = str.indexOf('foo') >= 0; // true
The indexOf method returns the character index where the first occurrence of the specified value is encountered, if not found, it returns -1.
CMS
2010-05-18 04:16:22
or to make it jquery like:var containsFoo = $("#foobar").text().indexOf('foo') >= 0; // true
Jacob
2010-05-18 04:21:46
@Jacob, nevertheless `text()` returns a `String`
Jacob Relkin
2010-05-18 05:12:07
A:
If your limited to jQuery which is just JavaScript... you can use the filter
var subgtSel = $("#jquerySelector").filter(function(i) {
// do your filter here
return $(this).attr("data-timestamp") <= subMsg.CreateDateTimeStamp;
});
the subgtSel becomes your new jQuery now with the relevant filter in the above. In the above, I am looking for all div elements that have an attribute that is less than the subMsg.CreateTimeStamp.
If your looking for a particular substring... you can do the following with jQuery right in the selector
var sel = $("#jquerySelector:contains('text')");
Jason Jong
2010-05-18 05:18:58
+1
A:
Why use 10 characters when 100 will do?
Here's the requested jQuery plugin:
jQuery.isSubstring = function(haystack, needle) {
return haystack.indexOf(needle) !== -1;
};
Usage:
$.isSubstring("hello world", "world")); // true;
Anurag
2010-05-18 06:52:36