This question already has an answer here:
How would I count the amount of spaces at the start of a string in C#?
example:
" this is a string"
and the result would be 4. Not sure how to do this correctly.
Thanks.
This question already has an answer here:
How would I count the amount of spaces at the start of a string in C#?
example:
" this is a string"
and the result would be 4. Not sure how to do this correctly.
Thanks.
Use
Enumerable.TakeWhile
,Char.IsWhiteSpace
andEnumerable.Count
Note that not only
" "
is a white-space but:White space characters are the following Unicode characters:
Or
While I like the Linq based answers here's a boring unsafe method that should be pretty fast
You can use LINQ, because
string
implementsIEnumerable<char>
:Try this: