PostgreSQL 判断字符串包含的几种方法
发布时间:2020-12-13 17:19:24 所属栏目:百科 来源:网络整理
导读:判断字符串包含的几种方法: 1. position(substring in string): postgres=# select position( 'aa' in 'abcd' ); position ---------- 0 (1 row)postgres=# select position( 'ab' in 'abcd' ); position ---------- 1 (1 row)postgres=# select position(
判断字符串包含的几种方法: 1. position(substring in string): postgres=# select position('aa' in 'abcd');
position ----------
0
(1 row)
postgres=# select position('ab' in 'abcd');
position ----------
1
(1 row)
postgres=# select position('ab' in 'abcdab');
position ----------
1
(1 row)
可以看出,如果包含目标字符串,会返回目标字符串笫一次出现的位置,可以根据返回值是否大于0来判断是否包含目标字符串。 2. strpos(string,substring): 该函数的作用是声明子串的位置。 postgres=# select strpos('abcd','aa');
strpos --------
0
(1 row)
postgres=# select strpos('abcd','ab');
strpos --------
1
(1 row)
postgres=# select strpos('abcdab','ab');
strpos --------
1
(1 row)
作用与position函数一致。 3. 使用正则表达式: postgres=# select 'abcd' ~ 'aa';
?column? ----------
f
(1 row)
postgres=# select 'abcd' ~ 'ab';
?column? ----------
t
(1 row)
postgres=# select 'abcdab' ~ 'ab';
?column? ----------
t
(1 row)
4. 使用数组的@>操作符(不能准确判断是否包含): postgres=# select regexp_split_to_array('abcd','') @> array['b','e'];
?column?
----------
f
(1 row)
postgres=# select regexp_split_to_array('abcd','') @> array['a','b'];
?column?
----------
t
(1 row)
注意下面这些例子: postgres=# select regexp_split_to_array('abcd','a'];
?column?
----------
t
(1 row)
postgres=# select regexp_split_to_array('abcd','c'];
?column?
----------
t
(1 row)
postgres=# select regexp_split_to_array('abcd','c','a','c'];
?column?
----------
t
(1 row)
可以看出,数组的包含操作符判断的时候不管顺序、重复,只要包含了就返回true,在真正使用的时候注意。 参考: http://francs3.blog.163.com/blog/static/40576727201262615549532/ (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
热点阅读