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. Simple string Function

Simple string Function

Scheduled Pinned Locked Moved C#
csharphelpquestionlearning
8 Posts 5 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.
  • I Offline
    I Offline
    Imran Adam
    wrote on last edited by
    #1

    Hi all, I am new to c#, learning lots iof new things everday! I would like help with a simple function, what few lines of code do i need in order to do the following: A String is passed into the function as parameter (string s), what would i need to add in the function for the number of words to be counted in that string and the result returned as a integer?? any ideas I know its a simple thing, but im new and would like some help.:-D

    Cheers :)

    S H 2 Replies Last reply
    0
    • I Imran Adam

      Hi all, I am new to c#, learning lots iof new things everday! I would like help with a simple function, what few lines of code do i need in order to do the following: A String is passed into the function as parameter (string s), what would i need to add in the function for the number of words to be counted in that string and the result returned as a integer?? any ideas I know its a simple thing, but im new and would like some help.:-D

      Cheers :)

      S Offline
      S Offline
      Stefan Troschuetz
      wrote on last edited by
      #2

      This should do the trick:

      inputString.Split(" ").Length


      "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

      www.troschuetz.de

      L 1 Reply Last reply
      0
      • S Stefan Troschuetz

        This should do the trick:

        inputString.Split(" ").Length


        "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

        www.troschuetz.de

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        it.all,depends;on_the_definition of a'word'. :)

        Luc Pattyn [My Articles]

        C I S 3 Replies Last reply
        0
        • L Luc Pattyn

          it.all,depends;on_the_definition of a'word'. :)

          Luc Pattyn [My Articles]

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          Luc Pattyn wrote:

          it.all,depends;on_the_definition of a'word'.

          Very clever.


          Upcoming events: * Glasgow: Geek Dinner (5th March) * Edinburgh: Web Security Conference Day for Windows Developers (12th April) My: Website | Blog | Photos

          1 Reply Last reply
          0
          • L Luc Pattyn

            it.all,depends;on_the_definition of a'word'. :)

            Luc Pattyn [My Articles]

            I Offline
            I Offline
            Imran Adam
            wrote on last edited by
            #5

            for example, if the string is: "programming is great" the result would be 3. i.e. 3 words hope this helps.

            Cheers :)

            L 1 Reply Last reply
            0
            • I Imran Adam

              for example, if the string is: "programming is great" the result would be 3. i.e. 3 words hope this helps.

              Cheers :)

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              Sure, but what about:

              r.s.v.p. 0, 1 or 4 words ?
              I don't know 3 or 4 words ?
              that's really great 3 or 4 words ?
              is programming great ? 3 or 4 words ?
              three words here 3 or 4 words ? (double space used!)

              So you really must specify in detail ... What constitutes a word, how do you recognize its begin and end, how do you treat composite words, what with abreviations, etc. BTW: I would say r.s.v.p. is five words (repondez s'il vous plait). :) -- modified at 15:43 Thursday 1st March, 2007

              Luc Pattyn [My Articles]

              1 Reply Last reply
              0
              • I Imran Adam

                Hi all, I am new to c#, learning lots iof new things everday! I would like help with a simple function, what few lines of code do i need in order to do the following: A String is passed into the function as parameter (string s), what would i need to add in the function for the number of words to be counted in that string and the result returned as a integer?? any ideas I know its a simple thing, but im new and would like some help.:-D

                Cheers :)

                H Offline
                H Offline
                Hayder Marzouk
                wrote on last edited by
                #7

                Hi, Here your function (I presume that each two words are separated with a space) : public int GetWordsCount(string s) { if (s==null) return 0; string trimmedString = s.Trim(); // To remove spaces at the begin/end string[] words = trimmedString.Split(' '); return words.Length; }

                1 Reply Last reply
                0
                • L Luc Pattyn

                  it.all,depends;on_the_definition of a'word'. :)

                  Luc Pattyn [My Articles]

                  S Offline
                  S Offline
                  Stefan Troschuetz
                  wrote on last edited by
                  #8

                  Luc Pattyn wrote:

                  it.all,depends;on_the_definition of a'word'.

                  Good point. I count three :)


                  "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

                  www.troschuetz.de

                  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