這篇文章主要介紹了PostgreSQL判斷字符串是否包含目標字符串的多種方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑑價值,需要的朋友可以參考下 |
PostgreSQL判斷字符串包含的幾種方法:
position(substring in string)函數:參數一:目標字符串,參數二原字符串,如果包含目標字符串,會返回目標字符串笫一次出現的位置,可以根據返回值是否大於0來判斷是否包含目標字符串
select position('aa' in 'abcd'); position ---------- 0 select position('ab' in 'abcd'); position ---------- 1 select position('ab' in 'abcdab'); position ---------- 1
strpos(string, substring)函數:參數一:原字符串,目標字符串,聲明子串的位置,作用與position函數一致。
select position('abcd','aa'); position ---------- 0 select position('abcd','ab'); position ---------- 1 select position('abcdab','ab'); position ---------- 1
如果包含目標字符串返回t,不包含返回f
select 'abcd' ~ 'aa' as result; result ------ f select 'abcd' ~ 'ab' as result; result ------ t select 'abcdab' ~ 'ab' as result; result ------ t
select regexp_split_to_array('abcd','') @> array['b','e'] as result; result ------ f select regexp_split_to_array('abcd','') @> array['a','b'] as result; result ------ t
注意下面這些例子:
select regexp_split_to_array('abcd','') @> array['a','a'] as result; result ---------- t select regexp_split_to_array('abcd','') @> array['a','c'] as result; result ---------- t select regexp_split_to_array('abcd','') @> array['a','c','a','c'] as result; result ---------- t
可以看出,數組的包含操作符判斷的時候不管順序、重複,只要包含了就返回true,在真正使用的時候注意。
到此這篇關於PostgreSQL判斷字符串是否包含目標字符串的文章就介紹到這了。