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.

1975 questions
52
votes
6 answers

How to use IndexOf() method of List
All the examples I see of using the IndexOf() method in List are of basic string types. What I want to know is how to return the index of a list type that is an object, based on one of the object variables. List employeeList = new…
omencat
  • 1,155
  • 1
  • 11
  • 18
43
votes
2 answers

Get index of object in a list using Linq

I am new to Linq. I have a Customers table.ID,FullName,Organization,Location being the columns. I have a query in Sqlite returning me 2500 records of customers. I have to find the index of the customer where ID=150 for example from this result set.…
RookieAppler
  • 1,517
  • 5
  • 22
  • 58
43
votes
9 answers

string.LastIndexOf() Bug?

does anyone know why: " ".LastIndexOf("<",0) returns -1 (wrong) while " ".LastIndexOf("<") returns 2 (right) and "".LastIndexOf("<",0) returns 0 (right) Is this a bug or am I misunderstanding the…
Florian Gl
  • 5,984
  • 2
  • 17
  • 30
42
votes
4 answers

indexOf and lastIndexOf in PHP?

In Java, we can use indexOf and lastIndexOf. Since those functions don't exist in PHP, what would be the PHP equivalent of this Java code? if(req_type.equals("RMT")) pt_password = message.substring(message.indexOf("-")+1); else pt_password…
Gaurav
  • 439
  • 1
  • 4
  • 3
39
votes
4 answers

LINQ indexOf a particular entry

I have an MVC3 C#.Net web app. I have the below string array. public static string[] HeaderNamesWbs = new[] { WBS_NUMBER, …
MikeTWebb
  • 9,149
  • 25
  • 93
  • 132
36
votes
10 answers

c# Array.FindAllIndexOf which FindAll IndexOf

I know c# has Array.FindAll and Array.IndexOf. Is there a Array.FindAllIndexOf which returns int[]?
Eric Yin
  • 8,737
  • 19
  • 77
  • 118
35
votes
12 answers

Javascript 2d array indexOf

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…
phoeberesnik
  • 385
  • 1
  • 6
  • 9
34
votes
4 answers

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

How do I do the in-place equivalent of strstr() for a counted string (i.e. not null-terminated) in C?
user541686
  • 205,094
  • 128
  • 528
  • 886
33
votes
3 answers

Why is indexOf failing to find the object?

I created an integer list and am trying to return the index of a specific value. The array is 3,8,2,5,1,4,7,6 and I want to return the indexOf(3), which should be 0. I've tried the following in the Eclipse Java Scrapbook after importing…
rishimaharaj
  • 1,644
  • 2
  • 19
  • 34
31
votes
8 answers

Is there an indexOf in javascript to search an array with custom compare function

I need the index of the first value in the array, that matches a custom compare function. The very nice underscorej has a "find" function that returns the first value where a function returns true, but I would need this that returns the index…
Peter T.
  • 2,927
  • 5
  • 33
  • 40
30
votes
4 answers

Uncaught TypeError: .indexOf is not a function

I am new to JavaScript and I'm getting an error as below. Uncaught TypeError: time.indexOf is not a function Gee, I really thought indexOf() really was a function. Here is a snippet of my code: var timeofday = new Date().getHours() + (new…
TerryO
  • 301
  • 1
  • 3
  • 3
29
votes
5 answers

Java indexOf method for multiple matches in String

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…
Eric
  • 1,191
  • 5
  • 14
  • 17
28
votes
7 answers

How can I correctly check if a string does NOT contain a specific word?

I am currently trying to figure out how to solve the above named problem. Specifically I want to check if the string does not contain the word "stream" both in capital and lowercase letters. Here's my code so far: if…
niccober
  • 343
  • 1
  • 3
  • 7
28
votes
5 answers

MySQL Second (or third) Index Of in String

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…
700 Software
  • 85,281
  • 83
  • 234
  • 341
27
votes
5 answers

How do I get the index of object in array using angular?

I need to have the index of an object in array so I can delete this part of the array. I tried using this: var index = this.urenRegistratie.indexOf(newDatum); But it keeps returning -1 and I don't know why this is happening. this is the part of the…
stan
  • 348
  • 1
  • 3
  • 13