It's not counting the delimiter characters.
It's counting all strings including empty strings.
Because you have two (or more) delimiter characters
between words - such as ',' and ' ' - splitting
on each of those delimiters results in empty
strings in the array.
If you do the operations individually and examine the
intermediate results in the debugger, you should see more
clearly what is happening. You can return the correct
count by filtering out empty strings.
public static int WordCount(this StringBuilder strbil)
{
char[] delimiterChars = { ' ', ',', '.', ':', ';', '!', '?', '-' };
var str = strbil.ToString();
var stra = str.Split(delimiterChars);
//return strbil.ToString().Split(delimiterChars).Length;
int count = 0;
foreach (var s in stra) if (s != "") count++;
return count;
}
Using a lambda might shorten the source code a little.
- Wayne