2

if i have array

array[0] = "jack"; array[1] = "jill"; array[2] = "lisa"; array[2] = "jackie";

and i want to find all elements with "ack" in it.

in this case it would return

"jack", "jackie".

what is the fastest way of doing this?

1
  • 1
    Are you sure you want the fastest, or do you want the easiest? Also, how many strings will be in your array in the real world? How many times will you be finding all elements in the array containing a particular substring? Is your array of words the same all the time, or is the substring the same all the time? The answers to these questions will prompt different solutions, if you really want the fastest one. Commented Oct 19, 2009 at 1:17

4 Answers 4

7
array.Where(s => s.Contains("ack"));

(Cheerfully ignoring any localisation / string collation / case sensitivity issues.)

Sign up to request clarification or add additional context in comments.

3 Comments

is there anyway to make this case insensitive
Not that it matters but my original answer was the same as this but with .ToLower() included. :(
Or s.IndexOf("ack", StringComparison.OrdinalIgnoreCase) >= 0. The benefit of using IndexOf >= 0 with a StringComparison is that it allows you to control whether the comparison is ordinal or culture-respecting (e.g. if a string contained an accented upper-case A, would it match the "a" of "ack"?).
1

This should be a little bit faster than a LINQ solution.

var list = new List<string>();
foreach(string s in array)
{
    if ((s ?? "").Contains("ack"))
    {
        list.Add(s);
    }
}

5 Comments

using String.Empty instead of "" might make it a little faster
I don't think there'll be any appreciable speed difference -- the LINQ Where operator results in almost exactly the same code, just with an additional iterator class stuck in to handle deferred enumeration. Good catch on handling nulls though!
That depends really. Some people consider shaving microseconds a good thing.
Although it is worth it to note that my crappy benchmarking tests showed it to be faster than the LINQ method, anyone know why it is? I used the following for the LINQ: array.Where(s => s.Contains("ack")).ToList()
Using Where creates a class called WhereArrayIterator which adds a layer of abstraction upon what I wrote here.
0

I don't really know C#. Here is a basic low-level approach in pseudo-code:

function boolean contains_string(string haystack, string needle)
  int needleIndex
  int haystackIndex
  for haystackIndex from 0 to haystack.length-needle.length
    for needleIndex from 0 to needle.length
      if haystack[haystackIndex+needleIndex] != needle[needleIndex]
        break
      end if
    end for
    if needleIndex == needle.length-1
      return TRUE
    end if
  end for
  return FALSE
end function

for each element in array
  if contains_string(element, "ack")
    new_array.push element
  end if
end for

Almost certainly contains bugs.

3 Comments

I guess you probably don't need to write your own Contains method. If feel a bit silly for posting this now. Maybe I should leave the C# questions to C# experts.
Looks like a mixture of VB, C, JavaScript. Very interesting.
It's too bad they didn't offer a reason.
0

I believe this should be faster than the LINQ solution.

IEnumerable<string> Containing(string[] xs, string sub) {
  foreach(string s in array)
  if (s.Contains(sub))
    yield return s;
}

I am assuming no null strings in xs.

1 Comment

I don't think this calls for yield. The odds are they will call .ToArray() anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.