indexof

indexOf in a string array

柔情痞子 提交于 2019-11-28 08:24:25
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 ? 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"); When the array is an array of objects, then: Object[] array = .. Arrays.asList(array).indexOf(someObj); Another alternative is org.apache.commons.lang.ArrayUtils.indexOf(...) which also has

indexOf() will not find a custom object type

橙三吉。 提交于 2019-11-27 23:11:47
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 ? indexOf requires the object as input. If it does not find the object you are passing in, it will return -1. You need to pass the object whose location in the arraylist you are

Get value of a string after a slash in JavaScript

瘦欲@ 提交于 2019-11-27 17:39:18
I am already trying for over an hour and cant figure out the right way to do it, although it is probably pretty easy: I have something like this : foo/bar/test.html I would like to use jQuery to extract everything after the last / . In the example above the output would be test.html . I guess it can be done using substr and indexOf() , but I cant find a working solution. T.J. Crowder 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

MySQL String Last Index Of

泄露秘密 提交于 2019-11-27 17:14:40
问题 I am able to use LOCATE to get the index of a character such as a . in www.google.com . Thus I can use substring to strip out everything before the first . . Is there a way I can look for the last / ? So I can get test.htm out of http://www.example.com/dev/archive/examples/test.htm ? I do not want to say the 6th slash, I want to say the last slash. Can this be done? 回答1: Use substring_index select substring_index('http://www.example.com/dev/archive/examples/test.htm','/',-1) 来源: https:/

strstr() for a string that is NOT null-terminated

陌路散爱 提交于 2019-11-27 17:12:47
问题 How do I do the in-place equivalent of strstr() for a counted string (i.e. not null-terminated) in C? 回答1: If you're afraid of O(m*n) behaviour - basically, you needn't, such cases don't occur naturally - here's a KMP implementation I had lying around which I've modified to take the length of the haystack. Also a wrapper. If you want to do repeated searches, write your own and reuse the borders array. No guarantees for bug-freeness, but it seems to still work. int *kmp_borders(char *needle,

How to trim a file extension from a String in JavaScript?

╄→гoц情女王★ 提交于 2019-11-27 16:49:40
For example, assuming that x = filename.jpg , I want to get filename , where filename could be any file name (Let's assume the file name only contains [a-zA-Z0-9-_] to simplify.). I saw x.substring(0, x.indexOf('.jpg')) on DZone Snippets , but wouldn't x.substring(0, x.length-4) perform better? Because, length is a property and doesn't do character checking whereas indexOf() is a function and does character checking. Marek Sapota If you know the length of the extension, you can use x.slice(0, -4) (where 4 is the three characters of the extension and the dot). If you don't know the length @John

window.location.indexOf not working in Javascript

末鹿安然 提交于 2019-11-27 16:09:06
Below is what I have. var myString = "http://localhost:8888/www.smart-kw.com/"; alert(myString.indexOf("localhost")); This give me alert... however if I change var myString = "http://localhost:8888/www.smart-kw.com/"; to var myString = window.location; , it won't work (I don't get alert). var myString = window.location; alert(myString.indexOf("localhost")); window.location is an accessor property, and getting its value gives you an object, not a string, and so it doesn't have an indexOf function. (It's perfectly understandable that people sometimes think it's a string, since when you set its

MySQL Second (or third) Index Of in String

╄→гoц情女王★ 提交于 2019-11-27 15:43:36
问题 What would be the simplest way to locate the index of the third space in a string. My goal is to get CCC out of this space separated list: AAAA BBBB CCCC DDDD EEE . where A and B and D are fixed length, and C is variable length, E F G are optional. In Java I would use indexof, with a starting point of 10 and that would get me the third space, but it seems that I cannot do that in MySQL, so I thought maybe I could find a 'third index of' function? 回答1: You would want to use SUBSTRING_INDEX

c# Array.FindAllIndexOf which FindAll IndexOf

本小妞迷上赌 提交于 2019-11-27 08:51:38
I know c# has Array.FindAll and Array.IndexOf . Is there a Array.FindAllIndexOf which returns int[] ? string[] myarr = new string[] {"s", "f", "s"}; int[] v = myarr.Select((b,i) => b == "s" ? i : -1).Where(i => i != -1).ToArray(); This will return 0, 2 If the value does not exist in the array then it will return a int[0]. make an extension method of it public static class EM { public static int[] FindAllIndexof<T>(this IEnumerable<T> values, T val) { return values.Select((b,i) => object.Equals(b, val) ? i : -1).Where(i => i != -1).ToArray(); } } and call it like string[] myarr = new string[] {

Java indexOf method for multiple matches in String

瘦欲@ 提交于 2019-11-27 08:19:08
I had a question about the indexOf method. I want to find multiple cases of "X" in a string. Suppose my string is "x is x is x is x", I want to find x in all of its index positions. But how do you do this for multiple cases? Is this even possible with indexOf? I did int temp = str.indexOf('x'); It find the first x. I tried to do a for loop where i is initialized to length of string and this did not work since I kept finding the first x over and over. for (int y = temp1; y >= 0;y-- ) { int temp = str.indexOf('x'); System.out.println(temp); } But this does not work. Am I supposed to use regex?