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
18
votes
1 answer

Swift: cannot convert value of type to @noescape while call indexOf, after filter using filter function

In Swift 2, I'm receiving an error: Cannot convert value of type '[String:AnyObject]' to expected argument type '@noescape ([String:AnyObject])' throws -> Bool" //today = NSDate() //array : [[String:AnyObject]] // I use if let because of we might…
Dima Deplov
  • 3,688
  • 7
  • 45
  • 77
18
votes
8 answers

How do I check if a char is a vowel?

This Java code is giving me trouble: String word = 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++) { …
Dinocv
  • 367
  • 2
  • 3
  • 10
18
votes
3 answers

C# string.IndexOf() returns unexpected value

This question applies to C#, .net Compact Framework 2 and Windows CE 5 devices. I encountered a bug in a .net DLL which was in use on very different CE devices for years, without showing any problems. Suddenly, on a new Windows CE 5.0 device, this…
Ergibt Sinn
  • 181
  • 1
  • 6
18
votes
2 answers

Javascript Performance: How come looping through an array and checking every value is faster than indexOf, search and match?

This came as a huge surprise for me, and I'd like to understand this result. I made a test in jsperf that is basically supposed to take a string (that is part of a URL that I'd like to check) and checks for the presence of 4 items (that are in fact,…
17
votes
6 answers

indexOf in a string array

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!…
chuck finley
  • 459
  • 3
  • 8
  • 16
17
votes
3 answers

How to find index of selected text in getSelection() using javascript?

I am trying to apply style to the text selected by the user(mouse drag) for which I need to get the start and end index of the selected text. I have tried using "indexOf(...)" method. but it returns the first occurrence of the selected substring. I…
Keekz
  • 185
  • 2
  • 10
17
votes
6 answers

Getting a collection of index values using a LINQ query

Is there a better way to do this? string[] s = {"zero", "one", "two", "three", "four", "five"}; var x = s .Select((a,i) => new {Value = a, Index = i}) .Where(b => b.Value.StartsWith("t")) .Select(c => c.Index); i.e. I'm looking for a more…
Guy
  • 65,082
  • 97
  • 254
  • 325
17
votes
3 answers

Javascript find index of word in string (not part of word)

I am currently using str.indexOf("word") to find a word in a string. But the problem is that it is also returning parts of other words. Example: "I went to the foobar and ordered foo." I want the first index of the single word "foo", not not the foo…
user759885
16
votes
1 answer

Why is List.IndexOf() a lot faster than List.Contains()?

I have List which has 150K elements. Average time of work IndexOf() is 4 times lower than Contains(). I tried to use List of int. For List of strings IndexOf is a bit faster. I found only one main difference, it's attribute TargetedPatchingOptOut.…
Pavel Belousov
  • 1,848
  • 2
  • 14
  • 22
16
votes
7 answers

JavaScript indexOf method with multiple values

I have an array which contains multiple same values ["test234", "test9495", "test234", "test93992", "test234"] Here I want to get the index of every test234 in the array For that I've tried Array.prototype.indexOf() method. But It only returns me 0…
user5672329
16
votes
1 answer

Uncaught TypeError: Undefined is not a function on indexOf

I currently have this code to check the website URL GET options for a specific ID, but whenever this code is run, I get a weird error: Uncaught TypeError: Undefined is not a function Here is my code: