indexof

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

匆匆过客 提交于 2019-11-30 23:23:59
问题 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 within foobar. I can not search for "foo " because sometimes it might be followed by a full-stop or comma (any non-alphanumeric character). 回答1: You'll have to use regex for this: > 'I went to the foobar and ordered foo.'.indexOf('foo') 14 > 'I

How to compare cells in JavaScript table to each other and test for equality? How does indexOf work?

a 夏天 提交于 2019-11-30 21:02:29
问题 I created a table in my HTML code. It has 9 columns and 13 rows. It gets filled up completely by a JavaScript loop that fills it with names of people from a few arrays. However, I want to add a validation step that makes sure that no two cells within a row hold the same value and that the value of a each cell does not repeat in the cell directly beneath it. Since I am only able to access the values of the cells of the table as a NodeList, I decided to make it into an array to use the IndexOf

Javascript: indexOf with Regular Expression

人走茶凉 提交于 2019-11-30 15:24:48
How can I check whether the page url contains a '#' character plus some random digits e.g. www.google.de/#1234 if( window.location.href.indexOf('#') > 0 ){ alert('true'); } Does indexOf support Regular Expressions? Use String.prototype.search to get the index of a regex: 'https://example.com/#1234'.search(/#\d+$/); // 20 And RegExp.prototype.test if used for boolean checks: /#\d+$/.test('https://example.com/#1234'); // true The regex used for these examples are /#\d+$/ which will match literal # followed by 1 or more digits at the end of the string. As pointed out in the comments you might

Getting Index of an item in an arraylist;

我们两清 提交于 2019-11-30 11:11:21
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 name? I know that there is an .indexOf() function. The parameter for this function is an object. To find the item that has a name, should I just use a for loop, and when the item is found, return the element position in the ArrayList ? Is there a better way? I think a for-loop should be a valid solution : public int getIndexByname(String pName) { for

LINQ indexOf a particular entry

孤街醉人 提交于 2019-11-30 11:06:57
I have an MVC3 C#.Net web app. I have the below string array. public static string[] HeaderNamesWbs = new[] { WBS_NUMBER, BOE_TITLE, SOW_DESCRIPTION, HARRIS_WIN_THEME, COST_BOGEY }; I want to find the Index of a given entry when in another loop. I thought the list would have an IndexOf. I can't find it. Any ideas? Well you can use Array.IndexOf : int index = Array.IndexOf(HeaderNamesWbs, someValue); Or just declare HeaderNamesWbs as an IList<string> instead - which can still be an array if you want: public static IList<string> HeaderNamesWbs = new[] { ... }; Note that I'd discourage you from

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

六月ゝ 毕业季﹏ 提交于 2019-11-30 08:55:43
问题 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, present in the string). It checks in 5 ways: plain indexOf; Split the string, then indexOf; regex search; regex match; Split the string, loop through the array of items, and then check if any of them matches the things it's supposed to match To my

Find word(s) between two values in a string

廉价感情. 提交于 2019-11-30 07:43:46
I have a txt file as a string, and I need to find words between two characters and Ltrim / Rtrim everything else. It may have to be conditional because the two characters may change depending on the string. Example: car= (data between here I want) ; car = (data between here I want) </value> Code: int pos = st.LastIndexOf("car=", StringComparison.OrdinalIgnoreCase); if (pos >= 0) { server = st.Substring(0, pos);.............. } This is a simple extension method I use: public static string Between(this string src, string findfrom, string findto) { int start = src.IndexOf(findfrom); int to = src

Javascript array contains/includes sub array

时光总嘲笑我的痴心妄想 提交于 2019-11-30 07:31:50
问题 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, 22]; So I want to know if master contains sub something like: if(master.arrayContains(sub) > -1){ //Do awesome stuff } So how can this be done in an elegant/efficient way? 回答1: With a little help from fromIndex parameter This solution features a closure

How to use IndexOf() method of List<object>

别等时光非礼了梦想. 提交于 2019-11-30 04:38:53
All the examples I see of using the IndexOf() method in List<T> 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<Employee> employeeList = new List<Employee>(); employeeList.Add(new Employee("First","Last",45.00)); I want to find the index where employeeList.LastName == "Something" int index = employeeList.FindIndex(employee => employee.LastName.Equals(somename, StringComparison.Ordinal)); Edit: Without lambdas for C# 2.0 (the original doesn't use LINQ or any .NET 3+ features, just the lambda

Javascript: indexOf with Regular Expression

倖福魔咒の 提交于 2019-11-29 22:15:02
问题 How can I check whether the page url contains a '#' character plus some random digits e.g. www.google.de/#1234 if( window.location.href.indexOf('#') > 0 ){ alert('true'); } Does indexOf support Regular Expressions? 回答1: Use String.prototype.search to get the index of a regex: 'https://example.com/#1234'.search(/#\d+$/); // 20 And RegExp.prototype.test if used for boolean checks: /#\d+$/.test('https://example.com/#1234'); // true The regex used for these examples are /#\d+$/ which will match