translate()
是一个系统函数,用于将字符串中的一组字符替换为另一组字符。
translate()
添加于 PostgreSQL 6.1。
用法
translate ( string text, from text, to text ) → text
如果 from 字符串中的每个字符出现在源 string 中,它将被 to 字符串中对应的字符替换。如果 from 字符串中的某个字符在 to 字符串中没有对应的字符(即 from 字符串比 to 字符串长),则该字符将被删除。
变更历史
- PostgreSQL 6.1
- 添加(提交 83978e1e)
示例
translate()
的基本执行,分别用 o
和 p
替换 b
和 r
的出现
postgres=# SELECT translate('foobar', 'br', 'po'); translate ----------- foopao (1 row)
from 字符串中没有在 to 字符串中对应字符的字符(在此示例中为 f
)将被删除
postgres=# SELECT translate('foobar', 'brf', 'po'); translate ----------- oopao (1 row)
translate()
可以处理多字节字符串
postgres=# SELECT translate('こん', 'ん', 'の'); translate ----------- この (1 row)
参考资料
- PostgreSQL 文档: 其他字符串函数