liquidsoap

Replace a substring in a string in liquidsoap


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."

Solution

  • 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".