469,920 Members | 2,384 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,920 developers. It's quick & easy.

StreamReader to read from string?

Hi,

Im using a code snippet that reads a text file from disc and processes it
using

stream = new StreamReader(filename);

Is it possible to assign a String instead of a file as the source of the
stream, and if yes, how?

Regards
Jesper.

Nov 17 '05 #1
6 3877
Jesper wrote:
Im using a code snippet that reads a text file from disc and processes it
using

stream = new StreamReader(filename);

Is it possible to assign a String instead of a file as the source of the
stream, and if yes, how?


Well, not for a StreamReader (easily) - but if all you need is a
*TextReader* (which is probable) just use StringReader instead.

Jon

Nov 17 '05 #2
Hello,
Yes you can, convert the string in to byte array using
System.Text.Encoding.ASCII.GetBytes method. Then pass those bytes to
MemoryStream class's constructor. You'll get and Stream object, but it
won't be of 'StreamReader' type.

HTH. Cheers.

Maqsood Ahmed - MCAD.net
Kolachi Advanced Technologies
http://www.kolachi.net

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #3
Something like this:

string text = "hello streams";
StreamReader reader = new StreamReader (new MemoryStream (
System.Text.Encoding.ASCII.GetBytes (text )));

Ab.
http://joehacker.blogspot.com

"Maqsood Ahmed" <ma***********@gmail.com> wrote in message
news:uF**************@TK2MSFTNGP14.phx.gbl...
Hello,
Yes you can, convert the string in to byte array using
System.Text.Encoding.ASCII.GetBytes method. Then pass those bytes to
MemoryStream class's constructor. You'll get and Stream object, but it
won't be of 'StreamReader' type.

HTH. Cheers.

Maqsood Ahmed - MCAD.net
Kolachi Advanced Technologies
http://www.kolachi.net

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #4
Maqsood Ahmed wrote:
Yes you can, convert the string in to byte array using
System.Text.Encoding.ASCII.GetBytes method. Then pass those bytes to
MemoryStream class's constructor. You'll get and Stream object, but it
won't be of 'StreamReader' type.


That's a really bad idea unless you've got solely ASCII characters.
You should at least use something which preserves Unicode characters -
eg Encoding.UTF8. (That also happens to be the default encoding used by
StreamReader...)

Jon

Nov 17 '05 #5
Hello,
Yes, it was just to *tell* :) it all depends upon the requirements of
the problem.

Maqsood Ahmed - MCAD.net
Kolachi Advanced Technologies
http://www.kolachi.net

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #6
Maqsood Ahmed <ma***********@gmail.com> wrote:
Yes, it was just to *tell* :) it all depends upon the requirements of
the problem.


Unfortunately, in my experience if you specify a "solution" which
doesn't explain its own limitations, people tend to use it without
questioning whether or not it's actually fit for purpose :(

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by andrewcw | last post: by
4 posts views Thread by Astronomically Confused | last post: by
2 posts views Thread by Bryan Dickerson | last post: by
1 post views Thread by Arpan | last post: by
4 posts views Thread by somequestion | last post: by
1 post views Thread by Sladan | last post: by
reply views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.