c# Array.FindAllIndexOf which FindAll IndexOf
string[] myarr = new string[] {"s", "f", "s"};
int[] v = myarr.Select((b,i) => b == "s" ? i : -1).Where(i => i != -1).ToArray();
This will return 0, 2
If the value does not exist in the array then it will return a int[0].
make an extension method of it
public static class EM
{
public static int[] FindAllIndexof<T>(this IEnumerable<T> values, T val)
{
return values.Select((b,i) => object.Equals(b, val) ? i : -1).Where(i => i != -1).ToArray();
}
}
and call it like
string[] myarr = new string[] {"s", "f", "s"};
int[] v = myarr.FindAllIndexof("s");
Get the list of index for a string in an array?
You can try the following,
string[] cars = {"Volvo", "BMW", "Volvo", "Mazda","BMW","BMW"};
//returns **{1,4,5}** as a list
var res = Enumerable.Range(0, cars.Length).Where(i => cars[i] == "BMW").ToList();
//joined items of list as 1,4,5
Label1.Text = string.Join(",", res);
In PowerShell is there a way to return the index of an array with substring of a string
You can use LINQ (adapted from this C# answer):
$myarray = 'herp', 'dederp', 'dedoo', 'flerp'
$substring = 'erp'
[int[]] $indices = [Linq.Enumerable]::Range(0, $myarray.Count).
Where({ param($i) $myarray[$i] -match $substring })
$indices
receives 0, 1, 3
.
As for what you tried:
$thisiswrong = @($myarray.IndexOf($substring))
System.Array.IndexOf
only ever finds one index and matches entire elements, literally and case-sensitively in the case of strings.
There's a more PowerShell-like, but much slower alternative, as hinted at by js2010's answer; you can take advantage of the fact that the match-information objects that Select-String
outputs have a .LineNumber
property, reflecting the 1
-based index of the position in the input collection - even if the input doesn't come from a file:
$myarray = 'herp', 'dederp', 'dedoo', 'flerp'
$substring = 'erp'
[int[]] $indices =
($myarray | Select-String $substring).ForEach({ $_.LineNumber - 1 })
Note the need to subtract 1
from each .LineNumber
value to get the 0
-based array indices, and the use of the .ForEach()
array method, which performs better than the ForEach-Object
cmdlet.
Get corners of a 2d array
From that, I want to get both the first and last non-null elements from the first and last rows each.
2D arrays are a pain in the ass, but they behave like one long array if you iterate them
var w = array.GetLength(1);
var h = array.GetLength(0);
var a = array.Cast<int?>();
var ff = a.First(e => e.HasValue);
var fl = a.Take(w).Last(e => e.HasValue);
var lf = a.Skip((h-1)*w).First(e => e.HasValue);
var ll = a.Last(e => e.HasValue);
Width is 9, Height is 3, Casting turns it into an enumerable of int?
which means your corners are the first non null, the last non null in the initial 9, the first non null after skipping 18 and the last non null
Related Topics
How to Get the Local MAChine Name in C#
Using Tfs API, How to Find the Comments Which Were Made on a Code Review
Reading from a Text File in C#
Incorrectly Aligned or Overlapped by a Non-Object Field Error
Should I Take Ilogger, Ilogger<T>, Iloggerfactory or Iloggerprovider for a Library
Event Signature in .Net -- Using a Strong Typed 'Sender'
How to Write Logs from Within Startup.Cs
How to Get All the Ad Groups for a Particular User
Reading Emails from Gmail in C#
Does Garbage Collector Call Dispose()
Wait for a While Without Blocking Main Thread
Does C# Support Project-Wide Default Namespace Imports Like Vb.Net
Datetime Tostring Issue with Formatting Months with "Mm" Specifier
Wpf Openfiledialog with the Mvvm Pattern