Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Count the unique number of words inside a string builder(Not letters...Words))

Count the unique number of words inside a string builder(Not letters...Words))

Scheduled Pinned Locked Moved C#
tutorial
11 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kanamala subin
    wrote on last edited by
    #1

    my sttring bulilder string would be like this: StringBuilder sb = new StringBuilder(); sb.Append("hi friends hi"); i want to check the count of "hi" in the sb so my final output would be like this. Count is 2. how to do that. thank u SUBIN

    D A C P M 5 Replies Last reply
    0
    • K kanamala subin

      my sttring bulilder string would be like this: StringBuilder sb = new StringBuilder(); sb.Append("hi friends hi"); i want to check the count of "hi" in the sb so my final output would be like this. Count is 2. how to do that. thank u SUBIN

      D Offline
      D Offline
      DaveyM69
      wrote on last edited by
      #2

      Probably not very efficient: Get the string by calling ToString(). Split by space to get an arry of words. You can then use Distinct to get unique words and/or Select to get a specific word

      Dave
      Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

      1 Reply Last reply
      0
      • K kanamala subin

        my sttring bulilder string would be like this: StringBuilder sb = new StringBuilder(); sb.Append("hi friends hi"); i want to check the count of "hi" in the sb so my final output would be like this. Count is 2. how to do that. thank u SUBIN

        A Offline
        A Offline
        Andreas X
        wrote on last edited by
        #3

        Something like this?

        StringBuilder sb = new StringBuilder();
        sb.Append("hi friends hi");

        Console.WriteLine("Count is " + "".ToString().Split(new []{" "}, StringSplitOptions.RemoveEmptyEntries).
        Where(w => w.Equals("hi", StringComparison.CurrentCultureIgnoreCase)).Count();

        Andreas Johansson
        Senior software developer at Tieto Sweden

        1 Reply Last reply
        0
        • K kanamala subin

          my sttring bulilder string would be like this: StringBuilder sb = new StringBuilder(); sb.Append("hi friends hi"); i want to check the count of "hi" in the sb so my final output would be like this. Count is 2. how to do that. thank u SUBIN

          C Offline
          C Offline
          Clifford Nelson
          wrote on last edited by
          #4

          Look at http://www.dotnetperls.com/word-count[^]

          public static int CountWords1(string s)
          {
          MatchCollection collection = Regex.Matches(s, @"[\S]+");
          return collection.Count;
          }

          J 1 Reply Last reply
          0
          • C Clifford Nelson

            Look at http://www.dotnetperls.com/word-count[^]

            public static int CountWords1(string s)
            {
            MatchCollection collection = Regex.Matches(s, @"[\S]+");
            return collection.Count;
            }

            J Offline
            J Offline
            Jasmine2501
            wrote on last edited by
            #5

            Yeah that's how I would do it, but copying your homework from Google is frowned upon. I think the OP should work to understand what the problem is, a method for solving it, and then the code for that method. Skipping to the last part won't help you pass the class.

            L 1 Reply Last reply
            0
            • J Jasmine2501

              Yeah that's how I would do it, but copying your homework from Google is frowned upon. I think the OP should work to understand what the problem is, a method for solving it, and then the code for that method. Skipping to the last part won't help you pass the class.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Jasmine2501 wrote:

              Skipping to the last part won't help you pass the class.

              That's why more and more companies are giving a coding-test when applying for a job. If the OP wants to cheat, he'll find that he has been cheated soon enough, owning a degree and not being able to use it :)

              Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

              J 1 Reply Last reply
              0
              • L Lost User

                Jasmine2501 wrote:

                Skipping to the last part won't help you pass the class.

                That's why more and more companies are giving a coding-test when applying for a job. If the OP wants to cheat, he'll find that he has been cheated soon enough, owning a degree and not being able to use it :)

                Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                J Offline
                J Offline
                Jasmine2501
                wrote on last edited by
                #7

                These days, I'm almost insulted when I haven't been asked to do a coding test. And, I did have a job recently where the interview should have clued me in to the fact they didn't know what the hell was going on... worked there for two months - everybody was real smart and legit with their skills - but I only did one productive thing the whole time. I should have known something was hinky in the interview when they did zero actual assessment of my skills.

                L 1 Reply Last reply
                0
                • J Jasmine2501

                  These days, I'm almost insulted when I haven't been asked to do a coding test. And, I did have a job recently where the interview should have clued me in to the fact they didn't know what the hell was going on... worked there for two months - everybody was real smart and legit with their skills - but I only did one productive thing the whole time. I should have known something was hinky in the interview when they did zero actual assessment of my skills.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Jasmine2501 wrote:

                  but I only did one productive thing the whole time.

                  That's when you start updating that resume again :^)

                  Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                  J 1 Reply Last reply
                  0
                  • L Lost User

                    Jasmine2501 wrote:

                    but I only did one productive thing the whole time.

                    That's when you start updating that resume again :^)

                    Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                    J Offline
                    J Offline
                    Jasmine2501
                    wrote on last edited by
                    #9

                    Oh heck yeah, I had another gig lined up before I quit from there. It's hard to stay unemployed if you have programming skills and you're being honest about it :)

                    1 Reply Last reply
                    0
                    • K kanamala subin

                      my sttring bulilder string would be like this: StringBuilder sb = new StringBuilder(); sb.Append("hi friends hi"); i want to check the count of "hi" in the sb so my final output would be like this. Count is 2. how to do that. thank u SUBIN

                      P Offline
                      P Offline
                      PIEBALDconsult
                      wrote on last edited by
                      #10

                      I'd use a Hashset.

                      1 Reply Last reply
                      0
                      • K kanamala subin

                        my sttring bulilder string would be like this: StringBuilder sb = new StringBuilder(); sb.Append("hi friends hi"); i want to check the count of "hi" in the sb so my final output would be like this. Count is 2. how to do that. thank u SUBIN

                        M Offline
                        M Offline
                        Marc Clifton
                        wrote on last edited by
                        #11

                        Something like:

                        sb.ToString().Split(' ').ToList().Distinct().Count()

                        That does (or should be close to) what your subject line wants to do. But that's different from what you wrote:

                        kanamala subin wrote:

                        i want to check the count of "hi" in the sb
                        so my final output would be like this.
                         
                        Count is 2.

                        Which is "count the number of instances of a word", for which you could do something like this:

                        Dictionary wordCount = new Dictionary();
                        sb.ToString().Split(' ').ToList().ForEach(w=>
                        {
                        if (wordCount.ContainsKey(w) ++wordCountCount[w];
                        else wordCount[w]=1;
                        }

                        Marc

                        Testers Wanted!
                        Latest Article: User Authentication on Ruby on Rails - the definitive how to
                        My Blog

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups