Why split string still counts delimiter characters?

Dmitriy 96 Reputation points
2021-01-26T23:53:06.41+00:00

I want to split a string of characters and count how many words in it. I wrote a code using the a sample from the Microsoft Documentation, but my program still counts all words, including the delimiter characters. What did I do wrong?
using System;
using System.Text;

namespace StringBuilderExtension
{
public static class StringBuilderExtensions {

    `public static int WordCount(this StringBuilder strbil)`  
    `{`              
        `char[] delimiterChars = { ' ', ',', '.', ':', ';', '!', '?', '-'};`  
        `return strbil.ToString().Split(delimiterChars).Length;`  
    `}`  

      
    `public static void Main(string[] args)`  
    `{`  
          
        `Console.WriteLine("Please enter your string: ");`  
        `string message = Console.ReadLine();`  

       ` StringBuilder strbil = new StringBuilder(message);`  
          
        `Console.WriteLine("The string \"" + strbil + "\" has " + strbil.WordCount() + " words.");`  

        `Console.ReadKey();`  
    `}`  
`}`  

}

60731-capture3.png

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,026 questions
{count} votes

Accepted answer
  1. WayneAKing 4,921 Reputation points
    2021-01-27T01:36:32.36+00:00

    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
    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Timon Yang-MSFT 9,591 Reputation points
    2021-01-27T02:00:11.35+00:00

    There is also a simple way to remove the empty string, c# provides an overload of the Split method.

            public static int WordCount(this StringBuilder strbil)  
            {  
                char[] delimiterChars = { ' ', ',', '.', ':', ';', '!', '?', '-' };  
                return strbil.ToString().Split(delimiterChars, StringSplitOptions.RemoveEmptyEntries).Length;  
            }  
    

    Split(String, StringSplitOptions)


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    2 people found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.