indexof

Finding second occurrence of a substring in a string in Java

六眼飞鱼酱① 提交于 2019-11-27 08:10:49
We are given a string, say, "itiswhatitis" and a substring, say, "is" . I need to find the index of 'i' when the string "is" occurs a second time in the original string. String.indexOf("is") will return 2 in this case. I want the output to be 10 in this case. Rohit Jain Use overloaded version of indexOf() , which takes the starting index (fromIndex) as 2nd parameter: str.indexOf("is", str.indexOf("is") + 1); int first = string.indexOf("is"); int second = string.indexOf("is", first + 1); This overload starts looking for the substring from the given index. I am using: Apache Commons Lang:

how to return the character which is at the index?

点点圈 提交于 2019-11-27 05:33:43
I know that i could return the index of a particular character of a string with indexof() function. But how i could return the character with the particular index? string s = "hello"; char c = s[1]; // now c == 'e' See also Substring , to return more than one character. Do you mean like this int index = 2; string s = "hello"; Console.WriteLine(s[index]); string also implements IEnumberable<char> so you can also enumerate it like this foreach (char c in s) Console.WriteLine(c); 来源: https://stackoverflow.com/questions/2416894/how-to-return-the-character-which-is-at-the-index

How do I check if a char is a vowel?

限于喜欢 提交于 2019-11-27 05:25:08
This Java code is giving me trouble: String word = <Uses an input> int y = 3; char z; do { z = word.charAt(y); if (z!='a' || z!='e' || z!='i' || z!='o' || z!='u')) { for (int i = 0; i==y; i++) { wordT = wordT + word.charAt(i); } break; } } while(true); I want to check if the third letter of word is a non-vowel, and if it is I want it to return the non-vowel and any characters preceding it. If it is a vowel, it checks the next letter in the string, if it's also a vowel then it checks the next one until it finds a non-vowel. Example: word = Jaemeas then wordT must = Jaem Example 2: word=Jaeoimus

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

狂风中的少年 提交于 2019-11-27 05:03:14
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. André C. Andersen 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 indexList; } I suppose you need to get all indices of the ArrayList where the object on that

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

时光毁灭记忆、已成空白 提交于 2019-11-27 02:31:20
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 } (array[i].id === 2) ? { indexes[2] = i } (array[i].id === 3) ? { indexes[3] = i } (array[i].id === 4) ? {

indexOf in a string array

限于喜欢 提交于 2019-11-27 02:10:46
问题 Is there anyway to get indexOf like you would get in a string. output.add("1 2 3 4 5 6 7 8 9 10); String bigger[] = output.get(i).split(" "); int biggerWher = bigger.indexOf("10"); I wrote this code but its returning an error and not compiling! Any advice ? 回答1: Use this ... output.add("1 2 3 4 5 6 7 8 9 10"); String bigger[] = output.get(i).split(" "); int biggerWher = Arrays.asList(bigger).indexOf("3"); 回答2: When the array is an array of objects, then: Object[] array = .. Arrays.asList

Javascript oddness with array of objects and indexOf

拥有回忆 提交于 2019-11-27 02:09:46
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 the function)? Array.indexOf() will only work on objects if the supplied object is exactly the same object you put in . An exact copy is

Why does IndexOf return -1?

若如初见. 提交于 2019-11-26 22:54:02
问题 I am learning Javascript and don't understand why the indexOf below returns -1: var string = "The quick brown fox jumps over the lazy dog"; console.log (string.indexOf("good")); 回答1: -1 means "no match found". The reason it returns -1 instead of "false" is that a needle at the beginning of the string would be at position 0, which is equivalent to false in Javascript. So returning -1 ensures that you know there is not actually a match. 回答2: -1 means no match found. "good" is not in that

Finding second occurrence of a substring in a string in Java

左心房为你撑大大i 提交于 2019-11-26 22:16:50
问题 We are given a string, say, "itiswhatitis" and a substring, say, "is" . I need to find the index of 'i' when the string "is" occurs a second time in the original string. String.indexOf("is") will return 2 in this case. I want the output to be 10 in this case. 回答1: Use overloaded version of indexOf(), which takes the starting index (fromIndex) as 2nd parameter: str.indexOf("is", str.indexOf("is") + 1); 回答2: int first = string.indexOf("is"); int second = string.indexOf("is", first + 1); This

indexOf() will not find a custom object type

那年仲夏 提交于 2019-11-26 21:20:53
问题 The following code does not give me right answer. class Point { int x; int y; public Point(int a,int b){ this.x=a;this.y=b; } } class A{ public static void main(String[] args){ ArrayList<Point> p=new ArrayList<Point>(); p.add(new Point(3,4)); p.add(new Point(1,2)); System.out.println(p.indexOf(1,2)); } } This gives -1 ; In general if arraylist of point is given, how can we find index of a particular point in in array ? 回答1: indexOf requires the object as input. If it does not find the object