indexof

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

孤人 提交于 2019-11-26 12:44:48
In javascript, is there an equivalent of String.indexOf() that takes a regular expression instead of a string for the first first parameter while still allowing a second parameter ? I need to do something like str.indexOf(/[abc]/ , i); and str.lastIndexOf(/[abc]/ , i); While String.search() takes a regexp as a parameter it does not allow me to specify a second argument! Edit: This turned out to be harder than I originally thought so I wrote a small test function to test all the provided solutions... it assumes regexIndexOf and regexLastIndexOf have been added to the String object. function

Trying to find all occurrences of an object in Arraylist, in java

Deadly 提交于 2019-11-26 11:25:54
问题 I have an ArrayList in Java, and I need to find all occurrences of a specific object in it. The method ArrayList.indexOf(Object) just finds one occurrence, so it seems that I need something else. 回答1: I don't think you need to be too fancy about it. The following should work fine: static <T> List<Integer> indexOfAll(T obj, List<T> list) { final List<Integer> indexList = new ArrayList<>(); for (int i = 0; i < list.size(); i++) { if (obj.equals(list.get(i))) { indexList.add(i); } } return

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

╄→尐↘猪︶ㄣ 提交于 2019-11-26 10:27:50
I'm trying to find the positions of all occurrences of a string in another string, case-insensitive. For example, given the string: I learned to play the Ukulele in Lebanon. and the search string le , I want to obtain the array: [2, 25, 27, 33] Both strings will be variables - i.e., I can't hard-code their values. I figured that this was an easy task for regular expressions, but after struggling for a while to find one that would work, I've had no luck. I found this example of how to accomplish this using .indexOf() , but surely there has to be a more concise way to do it? var str = "I learned

Javascript oddness with array of objects and indexOf

人盡茶涼 提交于 2019-11-26 09:59:00
问题 Not quite grasping what\'s going on here. Given the array (arr): [ { \"first_name\": \"Dan\", \"last_name\": \"Woodson\", \"id\": 1 }, { \"first_name\": \"Jen\", \"last_name\": \"Woodson\", \"id\": 2 }, { \"first_name\": \"Yoshi\", \"last_name\": \"Woodson\", \"id\": 3 } ] And the object (obj): { \"first_name\": \"Yoshi\", \"last_name\": \"Woodson\", \"id\": 3 } Why would arr.indexOf(obj) return -1 (especially since I retrieved the object from the array using it\'s \'id\' parameter earlier in

Javascript 2d array indexOf

烈酒焚心 提交于 2019-11-26 09:48:27
问题 I have a 2d array like this: var arr = [[2,3],[5,8],[1,1],[0,9],[5,7]]; Each index stores an inner array containing the coordinates of some element. How can I use Array.indexOf() to check if the newly generated set of coordinates is already contained in arr ? I want to push into arr if only the coordinate is NOT a duplicate. Here is my attempt that didn\'t work: if (arr.indexOf([x, y]) == -1) { arr.push([x, y]); } It looks like indexOf() doesn\'t work for 2d arrays... 回答1: You cannot use

In an array of objects, fastest way to find the index of an object whose attributes match a search

瘦欲@ 提交于 2019-11-26 08:44:46
问题 I\'ve been surfing around a little trying to find an efficient way to do this, but have gotten nowhere. I have an array of objects that looks like this: array[i].id = some number; array[i].name = some name; What i want to do is to find the INDEXES of the objects where id is equal to, for example, one of 0,1,2,3 or 4. I suppose I could just do something like : var indexes = []; for(i=0; i<array.length; i++) { (array[i].id === 0) ? { indexes[0] = i } (array[i].id === 1) ? { indexes[1] = i }

Cannot convert value of type &#39;Meme!&#39; to expected argument type &#39;@noescape (Meme) throws -> Bool&#39;

99封情书 提交于 2019-11-26 07:47:06
问题 Here is the code: @IBAction func deleteMeme(sender: UIBarButtonItem) { if let foundIndex = MemeRepository.sharedInstance.memes.indexOf(selectedMeme) { //remove the item at the found index MemeRepository.sharedInstance.memes.removeAtIndex(foundIndex) navigationController?.popViewControllerAnimated(true) The error happens at the .indexOf method at (selectedMeme) . Cannot convert value of type Meme! to expected argument type @noescape (Meme) throws -> Bool Meme! is a struct for my app. How do I

Is there a version of JavaScript&#39;s String.indexOf() that allows for regular expressions?

时光总嘲笑我的痴心妄想 提交于 2019-11-26 05:53:25
问题 In javascript, is there an equivalent of String.indexOf() that takes a regular expression instead of a string for the first first parameter while still allowing a second parameter ? I need to do something like str.indexOf(/[abc]/ , i); and str.lastIndexOf(/[abc]/ , i); While String.search() takes a regexp as a parameter it does not allow me to specify a second argument! Edit: This turned out to be harder than I originally thought so I wrote a small test function to test all the provided

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

雨燕双飞 提交于 2019-11-26 04:51:09
问题 I wrote this: public static class EnumerableExtensions { public static int IndexOf<T>(this IEnumerable<T> obj, T value) { return obj .Select((a, i) => (a.Equals(value)) ? i : -1) .Max(); } public static int IndexOf<T>(this IEnumerable<T> obj, T value , IEqualityComparer<T> comparer) { return obj .Select((a, i) => (comparer.Equals(a, value)) ? i : -1) .Max(); } } But I don\'t know if it already exists, does it? 回答1: The whole point of getting things out as IEnumerable is so you can lazily

Get the index of the nth occurrence of a string?

老子叫甜甜 提交于 2019-11-26 04:13:00
问题 Unless I am missing an obvious built-in method, what is the quickest way to get the n th occurrence of a string within a string? I realize that I could loop the IndexOf method by updating its start index on each iteration of the loop. But doing it this way seems wasteful to me. 回答1: That's basically what you need to do - or at least, it's the easiest solution. All you'd be "wasting" is the cost of n method invocations - you won't actually be checking any case twice, if you think about it.