3

Possible Duplicate:
How would you count occurences of a string within a string (C#)?

I am reading a file in line by line and need to count the number of tabs that each string contains. How can I get a count of the tabs i.e. \t from the string.

Thanks for your help

0

1 Answer 1

10
uint howManyTabs(string s) { return (uint)s.Count(ch => ch == '\t'); } 
Sign up to request clarification or add additional context in comments.

5 Comments

Why cast to uint?
Because it has to be a non-negative integer?
Because the function returns uint...
Why does the function return uint and not simply int?
@dtb, to make it explicit that the return value is never less than zero. That's what an unsigned int is for, after all. As far as I understand, things like Count return int in order to be CLS compliant, which is probably not an issue here.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.