Re: Number of Words in a String
- To: mathgroup at smc.vnet.net
- Subject: [mg102507] Re: Number of Words in a String
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Wed, 12 Aug 2009 04:31:56 -0400 (EDT)
On 8/11/09 at 4:02 AM, gregory.lypny at videotron.ca (Gregory Lypny) wrote: >Is this the simplest way to find the number of words in a string? >Seems a little complicated, and I can't seem to turn it into a >function because when I replace the string with the argument >placeholder myString_ I get an error message saying that a string is >expected in that spot. >Length[ReadList[StringToStream["The cat in the hat."], Word]] >Returns 5. It seems to me simpler to do: In:= Length@StringSplit["The cat in the hat."] Out= 5 This of course implicitly assumes words are any series of non-space characters. That assumption can be modified by using the second argument to StringSplit. But this doesn't seem to address your question about a placeholder. It is unclear to me what you are doing. Literally, myString_ is seen by Mathematica as a named pattern that matches anything, not a string. If I enter Length@StringSplit[myString_] I will see an error message since the argument supplied to StringSplit is not a string as expected.