0

I have a concurrent bag filled with strings like this:

var bag = new ConcurrentBag<string>();

The collection contains 100 strings from which I need to find a phrase like in following example:

"Galaxy S7" - search phrase

And lets say the titles are:

Samsung galaxy S4
Samsung galaxy S5
Samsung galaxy S6
Samsung galaxy S7

The output that I would get is:

Samsung galaxy S4
Samsung galaxy S5
Samsung galaxy S6

I've tried it with the method "Contains" that string has like following:

var _items = new ConcurrentBag<SearchItems>();
Parallel.For(0, xdoc.GetElementsByTagName("item").Count, index =>
{
    if (negative != null && negative != "")
    {
        if (!xdoc.GetElementsByTagName("title").Item(index).InnerText.Contains(negative))
        {
            _items.Add(new SearchItems() { 
                    CurrentPrice = Convert.ToDouble(xdoc.GetElementsByTagName("convertedCurrentPrice").Item(index).InnerText), 
                    ItemID = xdoc.GetElementsByTagName("itemId").Item(index).InnerText, 
                    Title = xdoc.GetElementsByTagName("title").Item(index).InnerText });
        }
    }
    else
    {
        _items.Add(new SearchItems() { 
                CurrentPrice = Convert.ToDouble(xdoc.GetElementsByTagName("convertedCurrentPrice").Item(index).InnerText), 
                ItemID = xdoc.GetElementsByTagName("itemId").Item(index).InnerText, 
                Title = xdoc.GetElementsByTagName("title").Item(index).InnerText });
    }
});

The problem is, contains is not case sensitive and if I try to exclude

s7

and

S7

I don't get same results... I need some solution that is not case sensitive :/

Can someone help me out?

Abion47
  • 22,211
  • 4
  • 65
  • 88
User987
  • 3,663
  • 15
  • 54
  • 115
  • 3
    Have you tried 'String.ToLower()'? Do this before doing anything with the string so it converts the string to lowercase, therefore when matching, the case will not matter. – MrStank Jan 19 '17 at 20:59
  • 1
    convert you string into lower case before running it into contains. String.ToLower() – Muhammad Saqlain Jan 19 '17 at 21:00
  • 1
    @KyleStankovich you mean like this: negative.ToLower() ? – User987 Jan 19 '17 at 21:00
  • 1
    https://msdn.microsoft.com/en-us/library/cc165449.aspx is all about string comparison. – CDove Jan 19 '17 at 21:00
  • @MuhammadSaqlain it still doens't works for some reason... maybe it works only for upper case letters? – User987 Jan 19 '17 at 21:02
  • 1
    @User987 Yeah. Here is a straight up example. `string str = "Hello World"; Console.WriteLine(str.ToLower());` The output world be **hello world** – MrStank Jan 19 '17 at 21:02
  • 1
    xdoc.GetElementsByTagName("title").Item(index).InnerText.ToLower().Contains(negative.ToLower()) – Muhammad Saqlain Jan 19 '17 at 21:04
  • @MuhammadSaqlain, Kyle, it still doesn't works, it adds the items to list which have S7 included in them.. Maybe this is due to the fact that it's running thrught a parallel loop instead of a regular one ? – User987 Jan 19 '17 at 21:05
  • If you need full text search, perhaps something like [Lucene.Net](https://lucenenet.apache.org/) would be a better choice. It is more involved, but will do what you want. – NightOwl888 Jan 19 '17 at 21:34

0 Answers0