我想写一个简单的功能,分裂一个 ByteString
成 [ByteString]
运用 '\n'
作为分隔符。我的尝试:
import Data.ByteString
listize :: ByteString -> [ByteString]
listize xs = Data.ByteString.splitWith (=='\n') xs
这会引发错误,因为 '\n'
是一个 Char
而不是一个 Word8
,这是什么 Data.ByteString.splitWith
期待。
如何将这个简单的角色变成一个 Word8
那 ByteString
会玩吗?
你可以使用数字文字 10
,但如果你想转换你可以使用的字符文字 fromIntegral (ord '\n')
(该 fromIntegral
需要转换 Int
那 ord
回到了 Word8
)。你必须导入 Data.Char
对于 ord
。
你也可以导入 Data.ByteString.Char8
,提供使用功能 Char
代替 Word8
一样的 ByteString
数据类型。 (的确,它有一个 lines
完全符合你想要的功能。)但是,这通常是 不 推荐,如 ByteString
小号 别 存储Unicode代码点(这是什么 Char
代表)而是原始八位字节(即 Word8
S)。
如果您正在处理文本数据,则应考虑使用 Text
代替 ByteString
。
你可以使用数字文字 10
,但如果你想转换你可以使用的字符文字 fromIntegral (ord '\n')
(该 fromIntegral
需要转换 Int
那 ord
回到了 Word8
)。你必须导入 Data.Char
对于 ord
。
你也可以导入 Data.ByteString.Char8
,提供使用功能 Char
代替 Word8
一样的 ByteString
数据类型。 (的确,它有一个 lines
完全符合你想要的功能。)但是,这通常是 不 推荐,如 ByteString
小号 别 存储Unicode代码点(这是什么 Char
代表)而是原始八位字节(即 Word8
S)。
如果您正在处理文本数据,则应考虑使用 Text
代替 ByteString
。