lpad()

一个用于在字符串左侧填充的系统函数

lpad() 是一个系统函数,用于通过使用一个或多个字符或字符串(默认为空格)填充字符串的左侧来扩展字符串。

lpad() 添加于 PostgreSQL 6.1

用法

lpad ( string text, length integer [, fill text ] ) → text

string 将通过在左侧填充空格,或者如果指定了 fill,则填充一个字符或字符串,来扩展到 length 个字符。如果在 fill 中指定了多个字符,则字符串将在填充时重复,并在必要时截断。

如果 string 已经长于 length,则 string 将被截断(在右侧)到 length

变更历史

示例

lpad() 的基本用法示例

postgres=# SELECT lpad('foo', 6);
  lpad  
--------
    foo
(1 row)

使用非默认填充字符

postgres=# SELECT lpad('foo', 6, '!');
  lpad  
--------
 !!!foo
(1 row)

使用填充字符串

postgres=# SELECT lpad('foo', 8, '!?');
   lpad   
----------
 !?!?!foo
(1 row)

尝试填充一个比指定填充长度更长的字符串

postgres=# SELECT lpad('foobar', 3);
 lpad 
------
 foo
(1 row)

尝试将字符串填充到负长度

postgres=# SELECT lpad('foobar', -3);
 lpad 
------
 
(1 row)

分类

系统函数

另请参阅

rpad()

反馈

提交关于“lpad()”的任何评论、建议或更正请 在此处