Questions tagged [indexof]

indexof is a method in programming languages like Java, C#, JavaScript, etc. that returns the first occurrence of a letter in a string, or the first occurrence of an element in a list.

indexof returns the first occurrence of a letter or a substring in a string, or the first occurrence of an element in a list.

1826 questions
361
votes
23 answers

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

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…
ma11hew28
  • 106,283
  • 107
  • 420
  • 616
294
votes
7 answers

Why doesn't indexOf work on an array IE8?

The below function works fine on Opera, Firefox and Chrome. However, in IE8 it fails on the if ( allowed.indexOf(ext[1]) == -1) part. Does anyone know why? Is there any obvious mistake? function CheckMe() { var allowed = new…
nLL
  • 5,572
  • 10
  • 48
  • 85
235
votes
18 answers

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

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]/ ,…
Pat
  • 34,832
  • 18
  • 68
  • 86
213
votes
12 answers

How to find the array index with a value?

Say I've got this imageList = [100,200,300,400,500]; Which gives me [0]100 [1]200 etc. Is there any way in JavaScript to return the index with the value? I.e. I want the index for 200, I get returned 1.
joedborg
  • 15,097
  • 30
  • 72
  • 112
213
votes
13 answers

Where is Java's Array indexOf?

I must be missing something very obvious, but I've searched all over and can't find this method.
Jamie
  • 3,501
  • 2
  • 20
  • 26
154
votes
12 answers

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

I wrote this: public static class EnumerableExtensions { public static int IndexOf(this IEnumerable obj, T value) { return obj .Select((a, i) => (a.Equals(value)) ? i : -1) .Max(); } public…
Jader Dias
  • 81,082
  • 147
  • 410
  • 611
150
votes
18 answers

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

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…
Petrov
  • 3,928
  • 6
  • 36
  • 58
137
votes
10 answers

Get value of a string after last slash in JavaScript

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…
r0skar
  • 7,059
  • 11
  • 47
  • 68
118
votes
17 answers

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

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…
Bungle
  • 17,944
  • 23
  • 75
  • 101
102
votes
10 answers

Get the index of the nth occurrence of a string?

Unless I am missing an obvious built-in method, what is the quickest way to get the nth 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…
PeteT
  • 16,600
  • 26
  • 87
  • 132
84
votes
7 answers

Finding second occurrence of a substring in a string in Java

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…
AmanArora
  • 1,885
  • 6
  • 16
  • 21
78
votes
6 answers

Index of element in NumPy array

In Python we can get the index of a value in an array by using .index(). But with a NumPy array, when I try to do: decoding.index(i) I get: AttributeError: 'numpy.ndarray' object has no attribute 'index' How could I do this on a NumPy array?
Marc Ortiz
  • 1,882
  • 4
  • 25
  • 45
53
votes
2 answers

How can I get a character in a string by index?

I know that I can return the index of a particular character of a string with the indexof() function, but how can I return the character at a particular index?
SmartestVEGA
  • 6,995
  • 18
  • 67
  • 118
51
votes
2 answers

How to get the Index of second comma in a string

I have a string in an Array that contains two commas as well as tabs and white spaces. I'm trying to cut two words in that string, both of them before the commas, I really don't care about the tabs and white spaces. My String looks similar to…
Sem0
  • 513
  • 1
  • 4
  • 6
50
votes
8 answers

Why Array.indexOf doesn't find identical looking objects

I have array with objects. Something Like this: var arr = new Array( {x:1, y:2}, {x:3, y:4} ); When I try: arr.indexOf({x:1, y:2}); It returns -1. If I have strings or numbers or other type of elements but object, then indexOf() works…
Jibla
  • 783
  • 2
  • 7
  • 12
1
2 3
99 100