Re: String to list
- To: mathgroup at smc.vnet.net
- Subject: [mg60985] Re: String to list
- From: fiaminamor at gmail.com
- Date: Thu, 6 Oct 2005 04:08:26 -0400 (EDT)
- References: <dht4il$ho4$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Sara wrote:
> How can I transform a string to a list??
> example:
> Str="Hi every one!" and the list af str=?????
You did not say what you want the list to be. Do you want the 3
words, or do you want the 13 characters in the string?
If you want a list of words so the result is {"Hi", "every", "one!"}
then you can use Mathematica's powerful commands for reading text
from a "stream." An input stream is usually a file on disk, but it
can be a string too. StringToStream[] makes a string into a stream,
so you can use the stream command ReadList[] on it.
Try this:
In[1]:= s = StringToStream["Hi every one!"] (* make it a stream *)
Out[1]= InputStream[String, 3]
In[2]:= ReadList[s, Word] (* read every word from the stream *)
Out[2]= {Hi, every, one!}
In[3]:= Close[s] (* I am finished with this stream *)
Out[3]= String
To control how Mathematica partitions the string into words, read
about the WordSeparators option for ReadList.