1

如何连接 2 个字符串?

在这里,我尝试定义一个将“f”连接到提供的字符串末尾的函数:

concat_f(bar) = output{
    item := ["f"]
    x := concat(bar, item)
    output := x
}

但是当我尝试使用它时(传入baras "123"):

hello {
    x := input.bar
    y := concat_f(x)
    y == "123f"
}

我得到这hello等于false

这是为什么?以及如何正确连接字符串?

这是有关该主题的文档。没找到有用的

4

2 回答 2

1

因为它检查条件 y == "123f" 并报告错误。

您的项目是数组,输入是字符串。我通常转换为数组或设置进行连接,字符串我觉得很难,可能我也是新的

https://play.openpolicyagent.org/p/sETVNU37B8

package play

concat_f(bar) = output{
    item := ["fine"]
    out := array.concat(bar, item)
    x := concat("", out)
    output := x
}

hello = y {
    x := input.bar
    y := concat_f(x)
    #y == "123fine"
}
于 2021-08-26T01:08:57.567 回答
0
concat_f(bar) = concat("", [bar, "f"])
于 2021-10-08T11:34:06.943 回答