stringhaskell

How to split a string in Haskell?


Is there a standard way to split a string in Haskell?

lines and words work great from splitting on a space or newline, but surely there is a standard way to split on a comma?

I couldn't find it on Hoogle.

To be specific, I'm looking for something where split "," "my,comma,separated,list" returns ["my","comma","separated","list"].


Solution

  • There is a package for this called split.

    cabal install split
    

    Use it like this:

    ghci> import Data.List.Split
    ghci> splitOn "," "my,comma,separated,list"
    ["my","comma","separated","list"]
    

    It comes with a lot of other functions for splitting on matching delimiters or having several delimiters.