indexof

Get the index of item in list based on value

可紊 提交于 2020-01-07 04:38:09
问题 The scenario is for a football league table. I can order the list by match win percentage and then by goals scored to determine their position in the league. I then use this ordering to get teams position in the league table using the IndexOf function. this.results = this.results.OrderByDescending(x => x.WinPercentage).ThenByDescending(x => x.Goals); this.results.Foreach(x => x.Position = this.results.IndexOf(x)); The problem arises when two teams (should be joint #1) have the same match win

Why javascript indexOf “empty string” always returning zero [duplicate]

淺唱寂寞╮ 提交于 2020-01-07 04:16:28
问题 This question already has answers here : indexOf empty string is zero. why? (1 answer) Java String.indexOf and empty Strings (5 answers) Closed 4 years ago . Why javascript indexOf method always return 0 for empty string. If i used any string indexOf("") always returns zero. Any reason for this. Explain the reason briefley. var str="jquery"; var index=str.indexOf("") \\ Always returns zero for any string. Thanks in advance. 来源: https://stackoverflow.com/questions/28692565/why-javascript

indexOf() function not working on arrays

百般思念 提交于 2020-01-07 02:52:18
问题 I have two divs as shown below: <div class="container"></div> <div class="container"></div> I created an array using these two divs: var containers = document.querySelectorAll(".container"); I also created a variable to represent the first div: var div1 = document.querySelectorAll(".container")[0]; When I use indexOf() to get the index of the first div in the array, I get an error: containers.indexOf is not a function Here is a snippet summarizing my problem: var containers = document

indexOf with multiple arguments

不打扰是莪最后的温柔 提交于 2020-01-03 20:56:06
问题 Is it possible to use multiple arguments when determining indexOf on an array? I want to determine if my array contains any of three integers. Important to note at this stage that the array will only have one value (if it has more, it won't reach this code block). array.indexOf(123 || 124 || 125) === 0 So if array = [123] then my indexOf should be 0 and therefore true . If array = [124] then my indexOf should be 0 and therefore true . What I am finding is happening is [123] works OK but it's

Javascript Arrays In IE 8 issue

天涯浪子 提交于 2020-01-01 19:39:08
问题 As per what I know, arrays in Javascript is nothing but the combination of methods and objects. Now my task is to display the values of array (say y_array ) I have used for(x in y_array) and then displayed the value. In mozilla and in IE its working fine, but in IE it displays the first element of array with index as indexOf and value is indexOf(obj, from) which i dont want. I tried if(x!='indexOf') { display the array value ; } It worked and things were fine but there is extensive use of

How can C#'s string.IndexOf perform so fast, 10 times faster than ordinary for loop find?

萝らか妹 提交于 2020-01-01 08:14:07
问题 I have a very long string ( 60MB in size) in which I need to find how many pairs of '<' and '>' are in there. I have first tried my own way: char pre = '!'; int match1 = 0; for (int j = 0; j < html.Length; j++) { char c = html[j]; if (pre == '<' && c == '>') //find a match { pre = '!'; match1++; } else if (pre == '!' && c == '<') pre = '<'; } The above code runs on my string for roughly 1000 ms . Then I tried using string.IndexOf int match2 = 0; int index = -1; do { index = html.IndexOf('<',

Hack to convert javascript number to UInt32

那年仲夏 提交于 2020-01-01 04:45:09
问题 Edit: This question is out of date as the Polyfill example has been updated. I'm leaving the question here just for reference. Read the correct answer for useful information on bitwise shift operators. Question: On line 7 in the Polyfill example of the Mozilla Array.prototype.indexOf page they comment this: var length = this.length >>> 0; // Hack to convert object.length to a UInt32 But the bitwise shift specification on Mozilla clearly states that the operator returns a value of the same

How to use IndexOf in JQuery

送分小仙女□ 提交于 2020-01-01 01:55:08
问题 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 IndexOf('4289||78843') I want this to check this numbers and if the number in the input field is not one of this, to echo error. Here's more which happens to die when one revisits the field. $('#Zip').blur(function(){ if (($(this).val().indexOf('0860') > -1)||($(this).val(

JavaScript string matching only at the start versus using indexOf?

耗尽温柔 提交于 2019-12-25 18:27:03
问题 I currently am matching user input as follows: user_input = "upload a file" if ( "upload a file".indexOf(user_input.toLowerCase()) > -1 ) {} This work fine but the problem is, it matches "a file" which I don't want. I want it to only match if the beginning is correct. This should match: "upload" "u" "upload a" This should not match, because the string does not match from the start: "a" "file" Are there any suggestions on how to make this happen with indexOf ? 回答1: indexOf return the index of

JavaScript string matching only at the start versus using indexOf?

风流意气都作罢 提交于 2019-12-25 18:26:04
问题 I currently am matching user input as follows: user_input = "upload a file" if ( "upload a file".indexOf(user_input.toLowerCase()) > -1 ) {} This work fine but the problem is, it matches "a file" which I don't want. I want it to only match if the beginning is correct. This should match: "upload" "u" "upload a" This should not match, because the string does not match from the start: "a" "file" Are there any suggestions on how to make this happen with indexOf ? 回答1: indexOf return the index of