How to pad a string?

While that’s certainly correct for a single string padding operation, it would still be useful to have padLeft and padRight functions (similar to the ones available in Groovy, for example) available in the String Manipulation nodes, for combining them with other string operations. This would avoid the need of having an additional node just for the padding, or moving the entire string manipulations into a Java snippet.

So how about adding the PadLeftManipulator and PadRightManipulator classes to the org.knime.base.node.preproc.stringmanipulation.manipulator package here:

https://github.com/knime/knime-core/tree/master/org.knime.jsnippets/src/org/knime/base/node/preproc/stringmanipulation/manipulator

I’d suggest the following function signatures (with default padding with spaces for the ones not defining a padding char):

padLeft(str, length)
padLeft(str, length, padding)
padRight(str, length)
padRight(str, length, padding)

Of course, already now you can do some padding in the String Manipulation node, without the need for a Java Snippet node, but it’s a bit cumbersome and not very readable, e.g. to pad an integer to four digits with 0s:

join(substr("0000", length(string($${INumber}$$))), string($${INumber}$$))
5 Likes