I'm trying to count the occurrence of a single digit to that in a string.
What I mean by this is The user types in a sentence in a multi line TextBox and below in a separate text field a single letter. I need to check how many times that single letter occurs in the sentence.
Currently, I have the input of the two text fields by using this
string InputSingleline = SingleLineTxtBox.Text;
string InputMultiline = MultiLineTxtBox.Text;
And I'm trying to count the occurance of input in InputSingleLine in MultiLineTxtBox by using this but it does not work.
int Count = InputMultiline.Count(f => f == SingleLineTxtBox);
InputSingleLine- you're trying to count the occurrences ofSingleLineTxtBox, which makes no sense. Next, you should try to count the occurrences of a character, not a string - so you want something likechar characterToFind = SingleLineTxtBox.Text[0];(after checking that there is actually a single character there). Then you can useint count = InputMultiLine.Count(f => f == characterToFind);. As an aside, I'd strongly recommend learning about C# naming conventions as early as possible to avoid getting into bad habits.SingleLineTxtBoxis empty? Usingchar characterToFind = SingleLineTxtBox.Text[0];will throwIndex was outside the bounds of the array.'