-
Oct 25th, 2021, 07:14 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] Find string in string[]
C# is a powerful tool, what I should choose to find index of a string in a string[] array:
string[] MyStrings = new string[6] { "test1", "test2", "test3", "test4", "test5", "test6" };
Code:
for (int i = 0; i <= MyStrings.GetUpperBound(0); i++)
{
if (MyStrings[i] == "test3")
{
return i;
}
}
Code:
int pos = Array.IndexOf(MyStrings, "test3");
Code:
int result = Array.FindIndex(MyStrings, x => x.Equals("test3"));
Last edited by DaveDavis; Oct 25th, 2021 at 07:18 AM.
-
Oct 25th, 2021, 09:42 AM
#2
Re: Find string in string[]
I guess small lists or arrays does not really make no difference.
Here is something you might want to read for future reference.
https://www.vbforums.com/showthread....ion&highlight=
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Oct 25th, 2021, 11:32 AM
#3
Re: Find string in string[]
Originally Posted by DaveDavis
C# is a powerful tool, what I should choose to find index of a string in a string[] array:
string[] MyStrings = new string[6] { "test1", "test2", "test3", "test4", "test5", "test6" };
Code:
for (int i = 0; i <= MyStrings.GetUpperBound(0); i++)
{
if (MyStrings[i] == "test3")
{
return i;
}
}
Code:
int pos = Array.IndexOf(MyStrings, "test3");
Code:
int result = Array.FindIndex(MyStrings, x => x.Equals("test3"));
If you are dealing with small amounts of data then it probably doesn't matter. If you have large amounts of data then it is possibly worth trying the different approaches and benchmarking them to see if there is an appreciable difference. If there isn't then just go for whichever approach you find easiest to read and maintain.
If you are going to benchmark then be aware it can be difficult to get benchmarking done correctly - https://benchmarkdotnet.org/articles/overview.html is a really nice tool for working with benchmarking .Net
-
Oct 25th, 2021, 07:00 PM
#4
Thread Starter
Fanatic Member
Re: Find string in string[]
Originally Posted by PlausiblyDamp
If you are dealing with small amounts of data then it probably doesn't matter. If you have large amounts of data then it is possibly worth trying the different approaches and benchmarking them to see if there is an appreciable difference. If there isn't then just go for whichever approach you find easiest to read and maintain.
If you are going to benchmark then be aware it can be difficult to get benchmarking done correctly - https://benchmarkdotnet.org/articles/overview.html is a really nice tool for working with benchmarking .Net
Thank for introducing the tools. The string[] array has only few hundreds items, I have tested there're no significant differences.
I will use option #2 because it is simple and has .NET taste.
-
Nov 7th, 2021, 04:03 PM
#5
Re: [RESOLVED] Find string in string[]
array.findindex is your best choice here. It is basically doing exactly what your first option was doing: looping through internally and looking for a match. It returns the index of the found element or -1 if not found. The 3rd option is designed for more complex matching and is not the choice for an array of primitives. You pass in an evaluation function and it gets called over and over for each element until a match is found. It is less efficient for primitives.
-
Nov 9th, 2021, 11:08 AM
#6
Re: [RESOLVED] Find string in string[]
I was going to say that IndexOf would be the best option because:
- High performance
- Backwards compatible back to .NET 1.1
- Less work than manually iterating over the collection
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|