我正在寻找replace-regexp-in-string
仅使用文字字符串,没有正则表达式的等价物。
(replace-regexp-in-string "." "bar" "foo.buzz") => "barbarbarbarbarbarbarbar"
但我想要
(replace-in-string "." "bar" "foo.buzz") => "foobarbuzz"
我尝试了各种replace-*
功能,但无法弄清楚。
编辑
作为对详尽答案的回报,我决定对它们进行基准测试(是的,我知道所有基准测试都是错误的,但它仍然很有趣)。
的输出benchmark-run
是(time, # garbage collections, GC time)
:
(benchmark-run 10000
(replace-regexp-in-string "." "bar" "foo.buzz"))
=> (0.5530160000000001 7 0.4121459999999999)
(benchmark-run 10000
(haxe-replace-string "." "bar" "foo.buzz"))
=> (5.301392 68 3.851943000000009)
(benchmark-run 10000
(replace-string-in-string "." "bar" "foo.buzz"))
=> (1.429293 5 0.29774799999999857)
replace-regexp-in-string 用带引号的正则表达式获胜。临时缓冲区做得非常好。
编辑 2
现在编译!必须进行 10 倍以上的迭代:
(benchmark-run 100000
(haxe-replace-string "." "bar" "foo.buzz"))
=> (0.8736970000000001 14 0.47306700000000035)
(benchmark-run 100000
(replace-in-string "." "bar" "foo.buzz"))
=> (1.25983 29 0.9721819999999983)
(benchmark-run 100000
(replace-string-in-string "." "bar" "foo.buzz"))
=> (11.877136 86 3.1208540000000013)
haxe-replace-string 看起来不错