1

Ok let's say I have an ObservableCollection<string> object. Within this object I have a variety of strings:

SomeString01
SomeString-02
somestring-03
SOMESTRING.04
aString

I want to take an input, we'll call it pattern and store it as a string from a User interface, and do some partial matching on the ObservableCollection. I need do to partial matching on the collection, and everything is going to be case insensitive. In the end I want to these compiled into a brand new ObservableCollection. So here are some example cases:

pattern = "SoME"

// RESULTS:
SomeString01
SomeString-02
somestring-03
SOMESTRING.04

/* --- */

pattern = "-0"

// RESULTS:
SomeString-02
somestring-03

/* --- */

pattern = "ING0"

// RESULTS:
SomeString01

pattern = "s"

// RESULTS:
SomeString01
SomeString-02
somestring-03
SOMESTRING.04
aString

What is the best approach for this in a ClickOnce application?

Urda
  • 5,460
  • 5
  • 34
  • 47
  • 2
    can't you use a `.Where(x => )` on the collection? – Gabe Moothart Oct 21 '10 at 14:37
  • @saurabh ... what is the best approach to do a case insensitive partial match on the collection. Did I do a poor job stating my question? – Urda Oct 21 '10 at 14:41
  • @Gabe I was trying to do that, but I'm having issues for case insensitivity. So I figured I was doing something the hard way / wrong way. – Urda Oct 21 '10 at 14:43

2 Answers2

1

Like Gabes answer in the comments.

but slightly more specific

.Where(x => x.IndexOf("Some",StringComparison.InvariantCultureIgnoreCase) != -1)
Dave Hanson
  • 515
  • 4
  • 11
  • What would be the difference of using that versus `.Where(x => x.IndexOf(pattern, StringComparison.OrdinalIgnoreCase >= 0)` ? Or is this 6 one way, half a dozen the other? – Urda Oct 21 '10 at 14:59
  • http://stackoverflow.com/questions/492799/difference-between-invariantculture-and-ordinal-string-comparision is a good writeup.. I don't know how to post links in comments yet effectively. Pretty much 6 one way, half a dozen the other – Dave Hanson Oct 21 '10 at 16:11
0

Ok I actually dug around more with Google, and found a better solution:

You could use IndexOf() and pass StringComparison.OrdinalIgnoreCase

string title = "STRING";
bool contains = title.IndexOf("string", StringComparison.OrdinalIgnoreCase) >= 0;

Even better is defining a new extension method for string:

public static bool Contains(this string source, string toCheck, StringComparison comp)
{
    return source.IndexOf(toCheck, comp) >= 0;
}

string title = "STRING";
bool contains = title.Contains("string", StringComparison.OrdinalIgnoreCase);

Contributed by: JaredPar

Source: Case insensitive 'Contains(string)'


SO now I have implemented it in my source as follows:

foreach (string source in SourceStrings)
{
    // Code for some pre-reqs here

    if (source.IndexOf(Pattern, StringComparison.OrdinalIgnoreCase) >= 0)
    {
        subset.Add(source);
    }

    // Finish up anything else I had to do here
}
Community
  • 1
  • 1
Urda
  • 5,460
  • 5
  • 34
  • 47