expression: string repeat

Hi there,
it’s my very first post here and I think it’s a basic question about string manipulation but after looking up in the forum and in the KNIME resources I didn’t find any hint.
Basically what I need to do is to add a leading zero before table data, based on the data length.
More specifically, I have a number which represent the month of the year, the months up to September are composed by 1 digit, while from October to December are composed by 2 digits (10, 11, 12).
I need to add a ‘0’ before the 1 digit month numbers.
In Excel I would achieve this simply by using a formula which retrieves the length of the string and adds a repetition of '0’s based on 2-stringLength, something like:

I’m trying to compose in KNIME a Column Expression node and I did find that the “lenght” function can retrieve the length of a string but I can’t find any repeat or if statement to perform the full expression I need.

Thanks in advance for your support.

Hi there,
after some more trial and error I succeded by using the node Number Formatterm by selencint my month number column, setting number of decimal places to -1 and leading character 0.
Then I used a Cross Joiner node to put back together my data.

In string manipulation node should be an option to “pad” your data with leading zeros.

1 Like

Hi @pippo82 , you can just use the padLeft() function from the String Manipulation, like this:
padLeft($column1$, 2, "0")

This will add leading zeroes to make up a string of length 2. So, 1 will become 01, and 11 will remain 11.


Thanks a lot @bruno29a!
I had to use first the Number To String to provide the String Manipulation a string but then your hint worked flawlessly.

1 Like

Hi @pippo82 , no problem. Happy to help.

You can convert to string with the string() function directly within the String Manipulation like this:
padLeft(string($column1$), 2, "0")

No need for Number To String :slight_smile:

1 Like

Oh, wow @bruno29a , love this nested formula! :slight_smile:


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.