string.LastIndexOf() Bug?

You are misunderstanding that particular overload of the LastIndexOf method. The docs state the following: The search starts at a specified character position and proceeds backward toward the beginning of the string. Note that it says backward. So, if you start at position 0, there is no “<” substring at that position or in front … Read more

How to use IndexOf() method of List

int index = employeeList.FindIndex(employee => employee.LastName.Equals(somename, StringComparison.Ordinal)); Edit: Without lambdas for C# 2.0 (the original doesn’t use LINQ or any .NET 3+ features, just the lambda syntax in C# 3.0): int index = employeeList.FindIndex( delegate(Employee employee) { return employee.LastName.Equals(somename, StringComparison.Ordinal); });

Why Array.indexOf doesn’t find identical looking objects

indexOf compares searchElement to elements of the Array using strict equality (the same method used by the ===, or triple-equals, operator). You cannot use === to check the equability of an object. As @RobG pointed out Note that by definition, two objects are never equal, even if they have exactly the same property names and … Read more

Get the index of the nth occurrence of a string?

You really could use the regular expression /((s).*?){n}/ to search for n-th occurrence of substring s. In C# it might look like this: public static class StringExtender { public static int NthIndexOf(this string target, string value, int n) { Match m = Regex.Match(target, “((” + Regex.Escape(value) + “).*?){” + n + “}”); if (m.Success) return … Read more

How to find indices of all occurrences of one string in another in JavaScript?

var str = “I learned to play the Ukulele in Lebanon.” var regex = /le/gi, result, indices = []; while ( (result = regex.exec(str)) ) { indices.push(result.index); } UPDATE I failed to spot in the original question that the search string needs to be a variable. I’ve written another version to deal with this case … Read more

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