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
>
>
- References:
- Number of Words in a String
- From: Gregory Lypny <gregory.lypny@videotron.ca>
- Number of Words in a String