MathGroup Archive 2009

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

Search the Archive

Re: Number of Words in a String

  • To: mathgroup at smc.vnet.net
  • Subject: [mg102523] Re: [mg102483] Number of Words in a String
  • From: Thomas Dowling <thomasgdowling at gmail.com>
  • Date: Wed, 12 Aug 2009 04:34:57 -0400 (EDT)
  • References: <200908110802.EAA21929@smc.vnet.net>

Hello,

1. One possibility is to use StringSplit

For example:

 In[105]= Length[StringSplit["The cat in a hat."]]

Out[105] = 5


2. You may wish to include a delimiter

In[107]= StringSplit["The cat in a hat, (not on the
mat)??.",Except[WordCharacter]..]

Out[107]=  {The,cat,in,a,hat,not,on,the,mat}

(and take the Length)

3.  Another way that works which I found in the documentation is StringCases

In[113]= Length@StringCases["The cat in a hat, (not on the
mat)??",WordCharacter..]

Out[113]= 9


Tom Dowling

On Tue, Aug 11, 2009 at 9:02 AM, Gregory Lypny
<gregory.lypny at videotron.ca>wrote:

> Hello everyone,
>
> 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.
>
> Gregory
>
>


  • Prev by Date: Re: Number of Words in a String
  • Next by Date: Re: Bug: ListPlot and Tooltip
  • Previous by thread: Re: Number of Words in a String
  • Next by thread: Re: Number of Words in a String