Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala用键/值列表替换字符串_Scala - Fatal编程技术网

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)}

请看一些模板引擎,