Simple string Function
-
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 :)
-
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 :)
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
-
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
it.all,depends;on_the_definition of a'word'. :)
Luc Pattyn [My Articles]
-
it.all,depends;on_the_definition of a'word'. :)
Luc Pattyn [My Articles]
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
-
it.all,depends;on_the_definition of a'word'. :)
Luc Pattyn [My Articles]
for example, if the string is: "programming is great" the result would be 3. i.e. 3 words hope this helps.
Cheers :)
-
for example, if the string is: "programming is great" the result would be 3. i.e. 3 words hope this helps.
Cheers :)
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]
-
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 :)
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; }
-
it.all,depends;on_the_definition of a'word'. :)
Luc Pattyn [My Articles]
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