string - 如何在 Haskell 中分割字符串?

标签 string haskell

Haskell 中有分割字符串的标准方法吗?

lineswords 在空格或换行符上分割效果很好,但是肯定有一种标准的方法来在逗号上分割吗?

我在 Hoogle 上找不到它。

具体来说,我正在寻找 split ",""my,comma,separated,list" 返回 ["my","comma","separated"的内容,“列表”]

最佳答案

请记住,您可以查找 Prelude 函数的定义!

http://www.haskell.org/onlinereport/standard-prelude.html

看那里,单词的定义是,

words   :: String -> [String]
words s =  case dropWhile Char.isSpace s of
                      "" -> []
                      s' -> w : words s''
                            where (w, s'') = break Char.isSpace s'

因此,将其更改为采用谓词的函数:

wordsWhen     :: (Char -> Bool) -> String -> [String]
wordsWhen p s =  case dropWhile p s of
                      "" -> []
                      s' -> w : wordsWhen p s''
                            where (w, s'') = break p s'

然后用你想要的任何谓词来调用它!

main = print $ wordsWhen (==',') "break,this,string,at,commas"

关于string - 如何在 Haskell 中分割字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4978578/

相关文章:

java - 使用不同的表达式分割字符串

c - char *str 为 null 和 str[0] == '\0' 有什么区别

python - 你如何使用 python 从字符串中提取 url?

c++ - 字符串按位或

haskell - 如何将 JPEG 图像旋转 45° 并将其保存回 Haskell 中的磁盘?

haskell - 如何在haskell中fmap元组的第一个元素

haskell - 管道的剩菜有什么好处?

java - StringBuilder 与 String(在确切情况下)

haskell - 断言是如何使用的?

unit-testing - 如何在 Haskell 中分离生产代码和测试代码