Replace a substring in a string in liquidsoap

162 Views Asked by At

Liquidsoap provides the string.replace function. But how should I use it? It seems to expect a function as the second argument that does the replacing.

I'd like to do something like this:

str = "Hello world."
str = string.replace(pattern="w", "W", str)
# str == "Hello World."
1

There are 1 best solutions below

0
stollr On BEST ANSWER

string.replace indeed expects a function for the first unlabeled parameter. The return value of that function will be used as a replacement.

Example:

str = "Hello world."
str = string.replace(pattern="w", (fun(_) -> "W"), str)
# str == "Hello World."

The inline arrow function fun(_) -> "W" is a function that will always return "W".