0

I have a function, that returns a number of character occurrences at text. But there is a problem: letter case matters. Function:

public static int GetOccurrences(String text, Char character)
    {
        return text.Count(x => x == character);
    }

For "Lorem ipsum dolor sit amet, pro eu erant semper ancillae" it will be 1 "L" and 3 "l", for example. Is it possible to ignore letter case?

AlIon
  • 347
  • 5
  • 25
  • Just make a lower case copy of the string, do your comparison, and return the int. – Bob G Apr 10 '17 at 19:00
  • 3
    Possible duplicate of [What is the correct way to compare char ignoring case?](http://stackoverflow.com/questions/1394877/what-is-the-correct-way-to-compare-char-ignoring-case) – Rufus L Apr 10 '17 at 19:07

1 Answers1

3

You can simply put them all to upper case when you compare them

text.Count(x => char.ToUpperInvariant(x) == char.ToUpperInvariant(character))
I.B
  • 2,925
  • 1
  • 9
  • 22
  • 1
    Check Jon's answer to this post regarding case sensitivity when doing comparisons: http://stackoverflow.com/questions/234591/upper-vs-lower-case – Rufus L Apr 10 '17 at 19:10
  • @RufusL That's very interesting, I didn't know about that. Would specifying the `CultureInfo` be better? – I.B Apr 10 '17 at 19:19
  • He answered a duplicate of this question here, which describes it better than I could: http://stackoverflow.com/questions/1394877/what-is-the-correct-way-to-compare-char-ignoring-case – Rufus L Apr 10 '17 at 19:40
  • @CNuts Sorry, meant to add the comment on the OP's post, was on a phone at the time – EJoshuaS - Stand with Ukraine Apr 11 '17 at 14:32