Pad a string in DolphinDB

28 Views Asked by At

I wonder if there are any alternatives to the Python rjust or zfill in DolphinDB.

I want to add zeros(0) on the specified side of a string until it reaches the specified length. How could I do?

2

There are 2 best solutions below

0
Hanwei Tang On

For string

lpad(`Hello, 12, `0);
0000000Hello
rpad(`Hello, 12, `0);
Hello0000000

For decimal

decimalFormat(123,"00000");
00123
0
Claire mcc On

You can use function lpad and rpad to pad a string with a specific set of characters.

Take lpad for example,

s = "123"
s = lpad(s, 5, "0")

The result is '00123'.