MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Number of Words in a String

  • To: mathgroup at
  • Subject: [mg102507] Re: Number of Words in a String
  • From: Bill Rowe <readnews at>
  • Date: Wed, 12 Aug 2009 04:31:56 -0400 (EDT)

On 8/11/09 at 4:02 AM, gregory.lypny at (Gregory Lypny)

>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[5]:= Length@StringSplit["The cat in the hat."]

Out[5]= 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


I will see an error message since the argument supplied to
StringSplit is not a string as expected.

  • Prev by Date: Re: Question concerning Manipulate
  • Next by Date: Re: Sticky scaling? (for printing)
  • Previous by thread: Re: Re: Number of Words in a String
  • Next by thread: Re: Number of Words in a String