Results 1 to 5 of 5
  1. #1
    javalover's Avatar
    Join Date
    May 2016
    Gender
    male
    Posts
    167
    Reputation
    10
    Thanks
    532

    .net - Counting number of words/spaces in a string

    This is a continuation of the following thread:

    https://www.mpgh.net/forum/showthread.php?t=1122156

    which I can't answer because the thread is closed. Why to open a discussions about this? In programming, people do not stop learning. Since I am interested to the argument, why to not talk about it.

    @Biesi closed the topic after his answer, without letting me reply.

    A pathetic stratagem
    If I was able to reply, the @OP probably had the possibility to use a valid method to do it. This was the important thing.

    I would like to quote his answer:

    telling people to create an unnecessarily large array of strings is just bullshit
    In C#, for 'large arrays' we would mean this:
    Code:
    char[] foo = new char[1024];
    with a large buffer. Am I using them in the methods I suggested?

    Code:
    public static int getWordsCount(string sentence)
            {
                System.Text.RegularExpressions.MatchCollection matchCollection = System.Text.RegularExpressions.Regex.Matches(sentence, @"[\S]+");
                return matchCollection.Count;
            }
    and:

    Code:
    sentence.Split(new char[] {'\r', '\n', ' '}, StringSplitOptions.RemoveEmptyEntries).Length;
    you are also specifying I'm using an array of strings. Where? There is no array of strings, there is just a static array of char in the second method (which has just 3 elements - so large) which I passed to the Split() function like delimiter.
    An array of char could actually mean just a string, not an array of strings.

    Also you should concern code, you are just counting the number of spaces in a string, not the words.
    Console.WriteLine(CountWords("This is a string"));

    Output will be 3 (spaces). And spaces can be simply counted doing the following:
    Code:
    string s = "This is a string";
                Console.WriteLine(s.Count(Char.IsWhiteSpace));
    which is conceptually the same thing.

    But let's do some benchmarks. These are my results in my machine:

    Code:
                             COUNTING SPACES IN A STRING
    
    
    CountWords() method (which actually counts spaces in a string) work (Biesi method)
    Number of spaces: 5
    Completed successfully.Time elapsed: 0,0017 ms
    
    String.Count(Char.IsWhiteSpace) method work (javalover method)
    Number of spaces: 5
    Completed successfully.Time elapsed: 0,0003 ms
    without any modification of your and my code. But you were anyway off-topic as @OP wrote he wanted to count number of words, not of spaces in a string.

    My suggestion of counting words using Split() method can be simplified yet. Here a simple way of doing it without literally using any array of char:

    Code:
    Console.WriteLine("this is my string hello mpgh".Split().Length);
    benchmark result:

    Code:
    Number of words: 6
    Completed successfully.Time elapsed: 0 ms
    and finally, using regex - benchmark result:
    Code:
    Number of words: 6
    Completed successfully.Time elapsed: 0,0003 ms
    Summary:


    don't you believe me? Test yourself without changing any part of the codes and using the Stopwatch class.

    n.b: Anyone can post here about his opinion, but flaming is not accepted.

  2. The Following User Says Thank You to javalover For This Useful Post:

    Nine11 (02-13-2017)

  3. #2
    Biesi's Avatar
    Join Date
    Dec 2011
    Gender
    male
    Posts
    4,993
    Reputation
    374
    Thanks
    8,808
    My Mood
    Twisted
    I'm not a minion, so I did not close the thread (and no, former staff does not mean staff).

    string.Split is creating an array of strings which you access the Length property to get the size of it. The array will be generated disregarding the fact that you're not using it as a variable. How about you try your glorious code on a text with a few 100.000 words and then attach a debugger to it

    I said one way would be to count the spaces followed by at least one letter, of course this could be improved. And no, string.Count is not the same as it would count multiple spaces.

    You were the one blaming me and saying the code I provided was "wrong" just because I did not return... but then providing bad practice examples

    So please stop whining
    Last edited by Biesi; 06-07-2016 at 05:18 AM.

  4. #3
    javalover's Avatar
    Join Date
    May 2016
    Gender
    male
    Posts
    167
    Reputation
    10
    Thanks
    532
    string.Split is creating an array of strings which you access the Length property to get the size of it
    Are you sure? Let's watch the decompiled code:



    what can you see now? String.Split() function internally creates an array of integers, and assignes the size on it with length of the string. The array of string is what it does return.
    Then, basing to the enumeration of StringSplitOptions, it calls InternalSplitOmitEmptyEntries() or InternalSplitKeepEmptyEntries().

    telling people to create an unnecessarily large array of strings is just bullshit
    you were letting people understand I said I had to create large array of strings explicitly, and so externally from Split() function, while it's actually implicitly returned from the Split() function.
    I suggest you being more specific.

    The array will be generated disregarding the fact that you're not using it as a variable
    I said one way would be to count the spaces followed by at least one letter
    To do what? Of course, to count spaces, which is always off-topic. The spaces should be counted in a string also if they aren't followed from a letter, that's also why it's wrong.

    How about you try your glorious code on a text with a few 100.000 words
    Of course.



    So, we can confirm my way is more performant.
    Imagine a string with a length of 10^40. The difference is that my method could be overkill and slower, your one would be the slowest one and obviously would not stop finishing.
    The user didn't say anything about counting the words in a string with an extralarge length. I don't think he has to count words in a string with 100.000 of length.

    And no, string.Count is not the same as it would count multiple spaces
    so, assuming your affermation, in this string:
    Code:
    my own string
    your code would count not more than 1 space?
    Or perhaps didn't you read what your interlocutor wrote?...
    Code:
    string hello = "This is a string";
    Console.WriteLine(CountWords(s)); // your method
    Console.WriteLine(s.Count(Char.IsWhiteSpace)); // mine
    your method's output: 3.
    my method's output: 3.

    The output will be the same with the small-and-wrong difference (of your code) that you are checking if the space is anyway followed by a letter.

    In the context of counting words you will need to fix a little thing, and so if you want to let your code returning the number of words, you should do:
    Code:
    return count + 1;
    and not:

    Code:
    return count;
    You were the one blaming me and saying the code I provided was "wrong" just because I did not return
    your code was just ot other than being literally wrong.


    So please stop whining
    n.b: Anyone can post here about his opinion, but flaming is not accepted.

  5. The Following User Says Thank You to javalover For This Useful Post:

    Nine11 (02-13-2017)

  6. #4
    MikeRohsoft's Avatar
    Join Date
    May 2013
    Gender
    male
    Location
    Los Santos
    Posts
    797
    Reputation
    593
    Thanks
    26,314
    This blowed Virtual Machines like Java or .NET consumes my Memory very much and then there are Developer like Javalover who is consuming more of my precious Memory for saving some CPU Time ^^
    Javalover's Method is faster, but you consume a lot of Memory with large Strings and Biesi's Method declare just 1 integer to clean up by the Garbage Collector...

  7. #5
    javalover's Avatar
    Join Date
    May 2016
    Gender
    male
    Posts
    167
    Reputation
    10
    Thanks
    532
    Quote Originally Posted by MikeRohsoft View Post
    there are Developer like Javalover who is consuming more of my precious Memory for saving some CPU Time ^^
    Javalover's Method is faster, but you consume a lot of Memory with large Strings and Biesi's Method declare just 1 integer to clean up by the Garbage Collector...
    Lol. It isn't my fault if you guys don't read what I write. Anyway, I've anticipated you as I've already explained this in my precedent post:

    Quote Originally Posted by javalover View Post
    Imagine a string with a length of 10^40. The difference is that my method could be overkill and slower, your one would be the slowest one and obviously would not stop finishing.
    The user didn't say anything about counting the words in a string with an extralarge length. I don't think he has to count words in a string with 100.000 of length.
    more precisely:
    Quote Originally Posted by javalover View Post
    The difference is that my method could be overkill and slower, your one would be the slowest one and obviously would not stop finishing.
    What does overkill mean?
    Complete definition:
    the amount by which destruction or the capacity for destruction exceeds what is necessary
    or simply:
    complete destruction
    now fit this definition to this context. Yes, we mean that.

    However I think the OP of that thread would have said if he had to count words in a large string with a length like 100.000 as it isn't often requested, but who knows.

  8. The Following User Says Thank You to javalover For This Useful Post:

    Nine11 (02-13-2017)

Similar Threads

  1. [WTS] Selling SECENTER.NET invitations [Limited number]
    By Dabadu in forum Selling Accounts/Keys/Items
    Replies: 2
    Last Post: 09-13-2015, 01:55 PM
  2. Numbers Game [Leme Count in peace] >.<
    By VindictusK in forum Spammers Corner
    Replies: 9
    Last Post: 06-04-2014, 01:05 AM
  3. [Suggestion] Have a certain number of post counts to post a hack
    By | ∞ | in forum Suggestions, Requests & General Help
    Replies: 6
    Last Post: 09-07-2010, 04:05 AM
  4. [Vb.net] Count the # of "Hi" in a textbox?
    By ppl2pass in forum Visual Basic Programming
    Replies: 1
    Last Post: 07-09-2010, 03:52 PM
  5. [Help]Number Count[Solved]
    By ppl2pass in forum Visual Basic Programming
    Replies: 35
    Last Post: 04-29-2010, 12:47 AM