如何检查数组中的字符串是否是另一个字符串的一部分?

huangapple go评论81阅读模式
英文:

How do I check if the strings inside an array are part of another string?

问题

我有一个字符串的动态数组。我想检查其中一些元素是否等于或有时也是输入字符串的一部分。

我想出了这个例子:

SELECT array_contains(array("ghi", "def", "%abc"), "foobabc")

然而,这不起作用并返回false

除了构建带有LIKE语句的大子句之外,还有其他方法可以做到这一点吗?

英文:

I have a dynamic array of strings. I would like to check if some of the elements are equal to or sometimes also part of an input string.

I came up with this example:

SELECT array_contains(array("ghi", "def", "%abc"), "foobabc")

This, however, does not work and returns false.

Is there any other way to do this besides building a large clause with LIKE statements?

答案1

得分: 0

array_contains函数在你的示例中检查输入字符串"foobabc"是否与数组["ghi", "def", "%abc"]中的任何元素完全匹配。由于没有完全匹配,所以该函数返回false。为了检查数组元素是否与字符串"fooabc"部分或完全匹配,您可以采用以下方法。

代码:

SELECT  array("ghi", "def", "abc") as array_col,transform(array("ghi", "def", "abc"), value ->  contains("fooabc",value)) as contains_col, array_max(transform(array("ghi", "def", "abc"), value ->  contains("fooabc",value))) as array_contains_col

transform函数创建一个新数组,其中包含原始数组中每个元素的布尔值。transform内部的contains函数用于检查输入字符串"fooabc"是否完全或部分包含数组中的每个元素。结果数组contains_col将包含[false, false, true],因为"abc"是数组中唯一一个是"fooabc"的子字符串的元素。然后使用array_max函数查找contains_col数组中的最大值,这个最大值是truearray_max函数的结果是一个标量值,分配给别名array_contains_col

如何检查数组中的字符串是否是另一个字符串的一部分?

这是检查输入字符串是否包含数组中任何元素的方法。

英文:

The array_contains function in your example checks if the input string "foobabc" is an exact match for any of the elements in the array ["ghi", "def", "%abc"]. Since there is no exact match, the function returns false. In order to check if array element has partial or exact match with the string "fooabc", you can follow the below approach.

Code:

SELECT  array("ghi", "def", "abc") as array_col,transform(array("ghi", "def", "abc"), value ->  contains("fooabc",value)) as contains_col, array_max(transform(array("ghi", "def", "abc"), value ->  contains("fooabc",value))) as array_contains_col

The transform function creates a new array that contains a boolean value for each element in the original array. The contains function within transform is used to check if the input string "fooabc" contains exactly or partially each element in the array. The resulting array contains_col will contain [false, false, true], since "abc" is the only element in the array that is a substring of "fooabc". The array_max function is then used to find the maximum value in the contains_col array, which is true. The result of the array_max function is a scalar value, which is assigned to the alias array_contains_col.

如何检查数组中的字符串是否是另一个字符串的一部分?

This is the approach to check if an input string contains any of the elements in the array.

huangapple
  • 本文由 发表于 2023年7月31日 23:19:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76805002.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定