简体   繁体   中英

What is the simplest way to convert a string to a stream?

Today, I use a memory stream :

new MemoryStream(Encoding.UTF8.GetBytes(str))

Is there a simpler / better way ?

Thank you !

That'll do it; there not really anything more direct than that, since you need the bytes (for the Stream API), hence need to go via an Encoding .

Note, however, that many APIs that take text-based data via a Stream will also accept a TextReader , and thus new StringReader(str) will be more direct.

尝试System.IO.StringReader类。

Don't know if there is simplest way or not, but I would say there is no better way (from the point of view of this question) to do this, as converting in memory, somehow you will need to convert it back. You need to know encodinig of the data you operate on.

In short: this is my prefered choice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM