Haskell用字符串替换字符串中的字符 [英] Haskell Replace characters in string with string
本文介绍了Haskell用字符串替换字符串中的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是此问题的扩展: Haskell替换字符串中的字符
我想调整以下表达式以用字符串替换char
I would like to tweak the following expression to replace a char with a string
let replaceO = map (\c -> if c=='O' then 'X'; else c)
最后,我将得到以下结果(XX可以是任意长度的字符串):
In the end, I would the following results (XX can be a string of any length):
replaceO "HELLO WORLD"
"HELLXX WXXRLD"
推荐答案
您可以使用 concatMap
:
let replace0 = concatMap (\c -> if c=='O' then "X" else "XX")
这篇关于Haskell用字符串替换字符串中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文