scala 用下划线连接替换字符串
scala replace string with underscore joined
我有一个关于如何替换字符串部分的一般性问题。
假设我有 2 个字符串:
a = "i am going to watch game of throne tonight on my throne"
b = "game_of_throne"
用 game_of_throne 替换《权力的游戏》的最有效方法是什么(即添加下划线以将其视为一个字符串主题)。如果我做类似正则表达式的事情:
val c = """_""".r.replaceAllIn(b," ").r
val c.replaceAllIn(a, c) How do I actually ask it to draw the underscore?
我试图避免拆分字符串,因为它通常会大大增加计算时间。
编辑:我有一百万对,所以我需要能够使用映射和变量 a 和 b。
a.replaceAll(b.replaceAll("_", " "), b)
不确定是否有更纯粹的 Scala 方法来执行此操作,但应该可以。
希望对您有所帮助。
object TestRegular extends App{
val a = "i am going to watch game of throne tonight on my throne"
val c = """game of throne""".r.replaceAllIn(a,"game_of_throne")
println(c)
}
我有一个关于如何替换字符串部分的一般性问题。
假设我有 2 个字符串:
a = "i am going to watch game of throne tonight on my throne"
b = "game_of_throne"
用 game_of_throne 替换《权力的游戏》的最有效方法是什么(即添加下划线以将其视为一个字符串主题)。如果我做类似正则表达式的事情:
val c = """_""".r.replaceAllIn(b," ").r
val c.replaceAllIn(a, c) How do I actually ask it to draw the underscore?
我试图避免拆分字符串,因为它通常会大大增加计算时间。
编辑:我有一百万对,所以我需要能够使用映射和变量 a 和 b。
a.replaceAll(b.replaceAll("_", " "), b)
不确定是否有更纯粹的 Scala 方法来执行此操作,但应该可以。
希望对您有所帮助。
object TestRegular extends App{
val a = "i am going to watch game of throne tonight on my throne"
val c = """game of throne""".r.replaceAllIn(a,"game_of_throne")
println(c)
}