Scala用键/值列表替换字符串
我有不同的弦像这样Scala用键/值列表替换字符串,scala,Scala,我有不同的弦像这样 " Hello *|USERNAME|*, to activate your account please click here *|ACTIVATION_LINK|* " ((USERNAME, Nick),(ACTIVATION_URL, http://whateverhost/activation_url)) mapKeyValues.map { x => bodyString.replaceAll(x._1, x._2) } 另一个例子 " Hell
" Hello *|USERNAME|*,
to activate your account please click here *|ACTIVATION_LINK|*
"
((USERNAME, Nick),(ACTIVATION_URL, http://whateverhost/activation_url))
mapKeyValues.map { x => bodyString.replaceAll(x._1, x._2) }
另一个例子
" Hello,
to reset your password please click here *|RESET_URL|*
"
对于第一个字符串,我将有一个键值列表,如下所示
" Hello *|USERNAME|*,
to activate your account please click here *|ACTIVATION_LINK|*
"
((USERNAME, Nick),(ACTIVATION_URL, http://whateverhost/activation_url))
mapKeyValues.map { x => bodyString.replaceAll(x._1, x._2) }
第二次
((RESET_URL, http://whateverhost/reset_url))
我想用键/值列表替换字符串,该列表可以具有可变长度,并且字符串中的键可能会出现多个
我试过这样的东西
" Hello *|USERNAME|*,
to activate your account please click here *|ACTIVATION_LINK|*
"
((USERNAME, Nick),(ACTIVATION_URL, http://whateverhost/activation_url))
mapKeyValues.map { x => bodyString.replaceAll(x._1, x._2) }
但问题是我得到了一个新列表,其中每一行都替换了一行键/值
有没有办法做到这一点?您可以使用foldLeft:
mapKeyValues
.foldLeft (bodyString) {case (acc,(key,value))=>acc.replaceAll(key, value)}
请看一些模板引擎,