简体   繁体   English

将字符串转换为流的最简单方法是什么?

[英]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.

相关问题 在字符串中获取年份的最简单方法是什么? - What is the simplest way to get the year in a string? 给定一个字符串数组,对它们进行随机排序的最简单方法是什么? - Given an array of string what is the simplest way to sort them randomly? 解析此表的最简单方法是什么: - What is the simplest way to parse this table: 应用主题的最简单方法是什么 - What is the simplest way to apply themeing 编码列表的最简单方法是什么<String>成纯字符串并将其解码回来? - What's the simplest way to encoding List<String> into plain String and decode it back? 做字符串包含使用Kendo Grid和Odata数据源对数字列进行过滤的最简单方法是什么? - What's the simplest way to do string contains filtering on a numeric column with a Kendo Grid and Odata data source? 区分Windows版本的最简单方法是什么? - What is the simplest way to differentiate between Windows versions? 根据条件对列表进行分区的最简单方法是什么? - What is the simplest way to partition a list based on a criteria? 检查字符串列表中元音的最简单方法 - Simplest way of checking vowels in a list of string 用Pipe以最简单的方式将字符串拆分成块? - Split string with Pipe for a chunk in simplest way?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM