2

I have two string arrays which I need to compare for the like strings.

string[] f1 = {"Ann", "mam", "far", "aBc"};
string[] f2 = {"ann", "nom", "far", "abc"};

Parallel.ForEach(f1, currenFile =>
{
    if (f2.Contains(currenFile, StringComparison.OrdinalIgnoreCase))
    {
        Console.WriteLine("matchfound"+currenFile);
    }
});

I am trying to see if f2 contains the word from f1 ignoring case. However intelisense has error on "StringComparison.OrdinalIgnoreCase" Saying

argument type System.StringComparission is not assignable to parameter type Systems.Collections.Generic.IEqualityComparer

Please let me know how can i fix this error.

Dmitry
  • 13,797
  • 6
  • 32
  • 48
J. Davidson
  • 3,297
  • 13
  • 54
  • 102
  • possible duplicate of [Case insensitive 'Contains(string)'](http://stackoverflow.com/questions/444798/case-insensitive-containsstring) – BradleyDotNET Jun 03 '14 at 23:39
  • 1
    Not the same. That is looking for a string in a string. Not a string in a string[] array. – paparazzo Jun 03 '14 at 23:47
  • Unless one is blind this is not same question. It looks like some people have trigger happy fingers to just keep making everything duplicate – J. Davidson Jun 03 '14 at 23:49
  • Your if statement looks for a string in a string, does it not? Sure you added the parallel foreach, but the essential question still seems to be the same to me. As an aside, I came up across that question while researching an answer to yours, so I'm not "trigger happy" at all, I found a question that asks the same thing (IMHO) with highly upvoted answers, and so marked this as a duplicate. – BradleyDotNET Jun 03 '14 at 23:55
  • 2
    I did not notice that f2 was an array, I apologize for the mistake. I have retracted my close vote (it was an honest mistake though :) ). The answers will be similar, but it is not a duplicate. – BradleyDotNET Jun 04 '14 at 00:09
  • @BradleyDotNet I understand some times i do same too. Thanks – J. Davidson Jun 04 '14 at 00:14
  • If you are looking for speed I would so use consider HashSet(StringComparer.OrdinalIgnoreCase) – paparazzo Jun 04 '14 at 00:17
  • @Blam speed is critical as the real data is very large. Thanks will try it out. – J. Davidson Jun 04 '14 at 00:25
  • You may get a 10x improvement in speed. HashSet.Contains is O(1) – paparazzo Jun 04 '14 at 00:50

1 Answers1

6

I fixed that error in VS2013 by using

StringComparer.OrdinalIgnoreCase

instead of

StringComparison.OrdinalIgnoreCase

Alternatively, you could try the following:

string[] f1 = { "Ann", "mam", "far", "aBc" };
string[] f2 = { "ann", "nom", "far", "abc" };
Parallel.ForEach(f1, currentFile =>
{
    if (f2.Any(comparisonFile => String.Compare(currentFile, comparisonFile, StringComparison.OrdinalIgnoreCase) == 0))
    {
        Console.WriteLine("matchfound" + currentFile);
    }
});
GEEF
  • 1,175
  • 5
  • 17
  • @BradleyDotNET that is because it is not a duplicate – paparazzo Jun 03 '14 at 23:51
  • @J. Davidson: I have your exact code with this one replacement in my Visual Studio browser right now and it compiles. Are you sure you are using StringComparer instead of StringComparison? – GEEF Jun 03 '14 at 23:54
  • @VP Well what version of .Net framework are you using? Is it on 4.5? I am on vs2012 and 4.5. – J. Davidson Jun 03 '14 at 23:57
  • @BradleyDotNET What part of looking for a string within a string is different than looking for a string in a string[] array is not clear? My name has an 'la' in it by my name is not 'la'. The duplicate is string and this is string[]. Do you know what an array is? – paparazzo Jun 03 '14 at 23:58
  • @BradleyDotNET f2 is an array. There is no array in that so called duplicate. – paparazzo Jun 04 '14 at 00:07
  • @J.Davidson MSDN says it works in 4.5, we are using VS2013 though to answer your question. Take a look at http://msdn.microsoft.com/en-us/library/system.stringcomparer(v=vs.110).aspx and see if it gives you anything. – GEEF Jun 04 '14 at 00:07
  • 1
    @Blam, I did not fully process the second array decleration, you are correct that this question is subtly different. Thank you for the effort to correct my mistake. – BradleyDotNET Jun 04 '14 at 00:09
  • @VP Thanks did a clean re-build and made the changes its working fine. – J. Davidson Jun 04 '14 at 00:23
  • @J.Davidson Glad to hear it – GEEF Jun 04 '14 at 00:27