Results 1 to 6 of 6

Thread: [RESOLVED] Find string in string[]

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2016
    Posts
    364

    Resolved [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.

  2. #2
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,065

    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=
    .

  3. #3
    Frenzied Member PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Newport, UK
    Posts
    1,494

    Re: Find string in string[]

    Quote Originally Posted by DaveDavis View Post
    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

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2016
    Posts
    364

    Re: Find string in string[]

    Quote Originally Posted by PlausiblyDamp View Post
    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.

  5. #5
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,609

    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.

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    10,593

    Re: [RESOLVED] Find string in string[]

    I was going to say that IndexOf would be the best option because:
    1. High performance
    2. Backwards compatible back to .NET 1.1
    3. Less work than manually iterating over the collection
    "Code is like humor. When you have to explain it, itís bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width