[英]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
. 没有比这更直接的东西,因为你需要字节(对于Stream
API),因此需要通过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. 但请注意,许多通过Stream
获取基于文本的数据的API 也会接受TextReader
,因此new StringReader(str)
将更直接。
尝试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. 简而言之:这是我的首选。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.