文字列を行に分ける
Data.Text.lines :: Text -> [Text]あるいはPrelude.lines :: String -> [String]を使います.
この場合,リストの要素の文字列は行末の改行記号を含まない,行の内容となります.
インポート
code: (haskell)
import qualified Data.Text as T
import qualified Data.Text.IO as T
サンプルデータ
code: (haskell)
sample = "One ma went to mow\nWent to mow a meadow\nOne man and his dog\nWent to mow a meadow\n"
評価例
code:_
>> :set -XOverloadedStrings
>> sample = "One man went to mow\nWent to mow a meadow\nOne man and his dog\nWent to mow a meadow\n"
>> sample
"One man went to mow\nWent to mow a meadow\nOne man and his dog\nWent to mow a meadow\n"
>> T.putStr sample
One man went to mow
Went to mow a meadow
One man and his dog
Went to mow a meadow
>> T.lines sample
>> length (T.lines sample)
4
>> putStr sample
One man went to mow
Went to mow a meadow
One man and his dog
Went to mow a meadow
>> lines sample
>> length (lines sample)
4
主な派生
bytestring: Data.ByteString.Char8.lines