How to get the index of an element in an IEnumerable?

I’d question the wisdom, but perhaps: source.TakeWhile(x => x != value).Count(); (using EqualityComparer<T>.Default to emulate != if needed) – but you need to watch to return -1 if not found… so perhaps just do it the long way public static int IndexOf<T>(this IEnumerable<T> source, T value) { int index = 0; var comparer = EqualityComparer<T>.Default; … Read more

Get value of a string after last slash in JavaScript

At least three ways: A regular expression: var result = /[^/]*$/.exec(“foo/bar/test.html”)[0]; …which says “grab the series of characters not containing a slash” ([^/]*) at the end of the string ($). Then it grabs the matched characters from the returned match object by indexing into it ([0]); in a match object, the first entry is the … Read more

Where is Java’s Array indexOf?

There are a couple of ways to accomplish this using the Arrays utility class. If the array is not sorted and is not an array of primitives: java.util.Arrays.asList(theArray).indexOf(o) If the array is primitives and not sorted, one should use a solution offered by one of the other answers such as Kerem Baydoğan’s, Andrew McKinlay’s or … Read more

Is there a version of JavaScript’s String.indexOf() that allows for regular expressions?

Instances of the String constructor have a .search() method which accepts a RegExp and returns the index of the first match. To start the search from a particular position (faking the second parameter of .indexOf()) you can slice off the first i characters: str.slice(i).search(/re/) But this will get the index in the shorter string (after … Read more

Why doesn’t indexOf work on an array IE8?

Versions of IE before IE9 don’t have an .indexOf() function for Array, to define the exact spec version, run this before trying to use it: if (!Array.prototype.indexOf) { Array.prototype.indexOf = function(elt /*, from*/) { var len = this.length >>> 0; var from = Number(arguments[1]) || 0; from = (from < 0) ? Math.ceil(from) : Math.floor(from); … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)