indexof

indexOf empty string is zero. why?

家住魔仙堡 提交于 2019-12-14 04:23:35
问题 why this is happening in javascript? 'abc'.indexOf('a'); //0 'abc'.indexOf(''); //0 while in the other falsy values, the value is -1: 'abc'.indexOf(); //-1 'abc'.indexOf(false); //-1 'abc'.indexOf(undefined); //-1 i have seen also those questions: Q1 didnt understand the answer in depth. Q2 in java and not javascript. 回答1: The answer, fundamentally, is: Because that's how the function is specified to behave. And it makes sense, from a certain perspective. The main bit related to returning 0

JAVA ArrayList cant find element via indexOf() [duplicate]

限于喜欢 提交于 2019-12-14 04:14:45
问题 This question already has answers here : Java ArrayList IndexOf - Finding Object Index (4 answers) Closed 2 years ago . Im writing a short program and I have a problem. There is a problem with more details. I have function public CardStck from(Card card) int pos = this.cardList.indexOf(card); card for this example is Card(value=5, color="D")- as u see on the debugging screen Even there is that card im searching for 5(D) (diamond 5 card) in this.cardList the value of pos is -1 (not found)

Unexpected result from String.indexOf function?

▼魔方 西西 提交于 2019-12-14 01:29:49
问题 I am literally pulling my hair out on this one... Here's the situation. I have two javascript strings as follows: dsName = "Test 1" replacementString = "Test " I'm trying to see if dsName starts with replacementString, with the following code: if(dsName.indexOf(replacementString) == 0) { // I never get here! } indexOf is returning -1!! How is this possible? I can put a breakpoint in Chrome script debugging right before that line and paste "dsName.indexOf(replacementString)" into the console

Wrong output from a while loop using indexOf [duplicate]

时间秒杀一切 提交于 2019-12-13 22:40:32
问题 This question already has answers here : While loop doesn't run a indexOf search (4 answers) Closed 5 years ago . I'm trying to find out how many times one string appears in another. For my testing, I'm using "ea" for wordOne and "Ilikedthebestontheeastbeachleast" for wordTwo. My output is returning 2 for my "appearance" variable, which should store how many times "ea" appears in wordTwo. It should return 3. I've tried messing with variable initializations, and trying to think of the math

Javascript indexOf object not found

房东的猫 提交于 2019-12-13 21:07:34
问题 I'm trying to find the index of my object inside an array in Javascript. This is the code I'm using, this returns -1 so it's not found. But it's there var index = this.state.users.indexOf(user); The object I'm comparing with is identical to the one's in the collection as shown in the picture below I'm using this code to compare the two console.log(this.state.users[0]); console.log(member); So I don't understand why this is giving a negative result 回答1: In javascript object compared by

Integrating Arduino and Processing - button counter

僤鯓⒐⒋嵵緔 提交于 2019-12-13 07:16:32
问题 I am looking to integrate Arduino controls and Processing Interface. In my Arduino code, there are three buttons attached to pin a1, a0 and d0 (all digitalRead). int x;// assigned to A0 input int y;// assigned to A1 input int z; //assigned to D0 input int votes[3]={0,0,0}; void setup() { // initialize the serial communication Serial.begin(9600); while(!Serial); } void loop() { // first we need to read the values from the BUTTONS x = digitalRead(A0); y = digitalRead(A1); z = digitalRead(0); if

Swift: get index of start of a substring in a string [duplicate]

拈花ヽ惹草 提交于 2019-12-13 00:22:51
问题 This question already has answers here : Swift is there a method that gives the index of a substring inside another string (2 answers) Closed 4 years ago . Is there a way to search a string for the index of the start of a substring? Like, "hello".indexOf("el") would return 1. Thanks 回答1: You can reach using the rangeOfString function that finds and returns the range of the first occurrence of a given string within a given range like in the following way: var text = "Hello Victor Sigler" if

How to get the Index of second comma in a string

一世执手 提交于 2019-12-12 07:39:03
问题 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 this: String s = "Address1 Chicago, IL Address2 Detroit, MI" I get the index of the first comma int x = s.IndexOf(','); And from there, I cut the string before the index of the first comma. firstCity = s.Substring(x-10, x).Trim() //trim white spaces

How javascript indexOf works differently with string and array?

不问归期 提交于 2019-12-12 04:54:10
问题 I have both string and array of strings. var strFruit = "Apple is good for health"; var arrayFruit = ["Apple is good for health"]; var strResult = strFruit.indexOf("Apple"); //strResult shows 0 var arrayResult = arrayFruit.indexOf("Apple"); // arrayResult shows -1 But if i use arrayFruit.indexOf("Apple is good for health") arrayResult shows 0. And my Question is why indexOf looks for exact match in Array element but not in string and how both search differ?. Jsfiddle P.S : Main reason to ask

How to remove item inside JPanel which is designated by an ArrayList

心已入冬 提交于 2019-12-12 03:19:50
问题 There is a JPanel with GridLayout which is randomly populated by JButtons created using an ArrayList. Each element of the array (a Tile) contains a character. It's supposed to be a tile removing game. However, I realised that the code I wrote didn't work, because once a tile is removed, the others' indexes change. A KeyListener is applied to the JPanel. This method is in my Model class, where the ArrayList is first created. getChar, in the Tile class, simply returns its character. public void