If you trying to find the number of spaces and tabs quickly, with no worry for optimization, another way to do is, utilize the exiting method of
String.Split[
^]
String.Split will return an array of strings with the split strings based on your criteria, then you need to count your array size and your are done.
str = "123 456";
string [] split = str.Split(new Char [] {' '});
spaces = split.Length - 1