add dash ("-") in string

Hi together,

I would like to add a dash (-) in certain places in a string.

Example: before 20230511 โ†’ after 2023-05-11.

Is this possible with the string manuplation?
Can someone help me with this? :slight_smile:

Thanks in advance

Hi @Manuel93

If you are only dealing with dates, the easiest is probably to use the -String to Date&Time- node:

You can use the โ€œGuess data type and formatโ€ button.

This converts the string into knime date type format.

If you need it as a string, you can then use the -Date&Time to String- node afterwards to convert it back to string type with format yyyy-MM-dd:

Hope that is useful for you :slight_smile:
Heather

5 Likes

Like @HeatherPikairos already mentioned if you are dealing with Dates you should/could use the Date and Time Nodes since its also easier to work with this Data Type later on. If it is not about dates you could use the String Manipulation node and use the substr(str, start, lenght) method which is a little bit inflexible if the input changes or you could also try to work with regular expressions.

2 Likes

If someone is going to substr route that @laaaarsi mentioned, mind that you need to join all separate pieces of the puzzle together again with a join() function. Example:

join(substr($column1$,0,4),"-",substr($column1$,4,2),"-", substr($column1$,6,2))

3 Likes

With a regular expression like this regexReplace($column1$, "(\\d{4})(\\d{2})(\\d{2})", "$1-$2-$3") this can also be achieved. The regular expression pattern (\\d{4})(\\d{2})(\\d{2}) captures three groups of digits: the year, month, and day. The replacement string $1-$2-$3 refers to the captured groups and inserts hyphens between them

4 Likes

Thank you very much @laaaarsi this solution works very well. Thanks also to the others for the quick help. Itโ€™s really great how fast you get help here :slight_smile:

1 Like

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