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
27
votes
2 answers

Cannot convert value of type 'Meme!' to expected argument type '@noescape (Meme) throws -> Bool'

Here is the code: @IBAction func deleteMeme(sender: UIBarButtonItem) { if let foundIndex = MemeRepository.sharedInstance.memes.indexOf(selectedMeme) { //remove the item at the found index …
Patrick.Bellot
  • 303
  • 4
  • 10
26
votes
6 answers

Why does IndexOf return -1?

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"));
Adnan Khan
  • 655
  • 2
  • 8
  • 20
26
votes
2 answers

IE: indexOf results in "object doesn't support this property or method"

I have the following if statement: if (buyArray.indexOf(dealWith,0) != -1){ Which is breaking in ie (ie 8 on XP) with "object doesn't support this property or method". Anyone have a work around for this?
Fraser
  • 14,036
  • 22
  • 73
  • 118
25
votes
1 answer

MySQL String Last Index Of

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

Java negative indexOf (counting from the end [length()] )

Is there any way in Java to found indexOf of a char starting from the end and having length() as a reference as other languages do? new String("abcd").reverseIndexOf("d"(,[4 or -0])) or new String("abcd").indexOf("d",-0) // Should return a…
Whimusical
  • 6,401
  • 11
  • 62
  • 105
24
votes
5 answers

How to correctly use JavaScript indexOf in a date array

Here is the code: var collection = [new Date(2014, 11, 25), new Date(2014, 11, 24)]; var d=new Date(2014, 11, 24); var idx= collection.indexOf(d); I guess the variable idx should have a value of 1 since it is the second value in the array…
Blaise
  • 21,314
  • 28
  • 108
  • 169
23
votes
4 answers

Getting the object array index in jq

I have a json object that looks like this (prodused by i3-msg -t get_workspaces. [ { "name": "1", "urgent": false }, { "name": "2", "urgent": false }, { "name": "something", "urgent": false } ] I am trying to use…
xeor
  • 5,301
  • 5
  • 36
  • 59
23
votes
3 answers

Is it a bad idea to use indexOf inside loops?

I was studying big O notation for a technical interview and then I realized that javascript's indexOf method may have a time complexity of O(N) as it traverses through each element of an array and returns the index where its found. We also know…
Vatsal
  • 2,068
  • 4
  • 21
  • 24
23
votes
8 answers

Remove domain information from login id in C#

I would like to remove the domain/computer information from a login id in C#. So, I would like to make either "Domain\me" or "Domain\me" just "me". I could always check for the existence of either, and use that as the index to start the…
Dan
  • 1,205
  • 3
  • 11
  • 9
23
votes
5 answers

javascript indexOf not working as expected with array

I am getting -1 from myarray.indexOf(element) even when element appears to be in myarray. Here's some code snippets: function createChangeRecord( old_array, new_array ) { var nds = new_array.slice(0,new_array.length); var el, idx; if…
baixiwei
  • 1,009
  • 4
  • 20
  • 27
21
votes
13 answers

Javascript array contains/includes sub array

I need to check if an array contains another array. The order of the subarray is important but the actual offset it not important. It looks something like this: var master = [12, 44, 22, 66, 222, 777, 22, 22, 22, 6, 77, 3]; var sub = [777, 22,…
Victor Axelsson
  • 1,420
  • 1
  • 15
  • 32
21
votes
1 answer

How to use IndexOf in JQuery

if($('#this').val().indexOf('4289')){ Do something else Do something. This works only with that 4289, When I try to add other numbers to be indexed next to it using 'or', it doesn't work. How should I put other number. E.g…
Blessing Thinker
  • 261
  • 1
  • 4
  • 14
21
votes
6 answers

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

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.
missrg
  • 585
  • 2
  • 8
  • 19
20
votes
1 answer

Why can't IndexOf find the character N in combination with Y in hungarian culture?

The IndexOf function called on a string returns -1, while there definitely is a match. string sUpperName = "PROGRAMOZÁSI NYELVEK II. ADA EA+GY. (BSC 08 A)"; string sUpperSearchValue = "N"; sUpperName.IndexOf(sUpperSearchValue); // Returns…
Dark
  • 335
  • 1
  • 5
20
votes
8 answers

Getting Index of an item in an arraylist;

I have a Class called AuctionItem. The AuctionItem Class has a method called getName() that returns a String. If I have an ArrayList of type AuctionItem, what is the best way to return the index of an item in the ArrayList that has a specific…
user2351151
  • 725
  • 4
  • 12
  • 16