Java Snippet Simple

Hi,

i have written a code in the method body (see below).

String textField;
textField = $Januar$.replace(“<0,001”,“1”);
textField = textField.replace(“n.b.”,“1”);
return textField;

But just want not only use in this case the month January but many other columns too like February, March etc.
But I cannot find the settings in this node and also the right instruction to replace a text in several columns.

Thanks.
Ol

Hi Ol,
Welcome to the KNIME Forum! For the Java Snippet (simple) that is difficult, but these simple replacement operations can also be performed using the String Manipulation (Multi Column) node. That should do the trick!
Kind regards,
Alexander

1 Like

Hi Alexander,

thanks for the fast answer. I tried it before (see below).
grafik

But I couldn’t find a way to put more instructions than one single replace instruction. How can I put more than 1 instruction to these following one single line?

Thanks a lot.

Ol

Hi @Ol_Broenner,
You can simply nest multiple function calls. Like this:

replace(replace($mycol$, "ABC", "DEF"), "XYZ", "123")

Kind regards,
Alexander

Hi Alexander,
I saw this answer with the nest multiple function in the forum too. But I have round about 8-10 multiple function calls to replace. The nesting possibility would not look very clear in this case. Is there not an easier way?
Thanks.
Ol

Hi Ol,
I agree nesting so many functions may be a bit messy. But if you format it like this, it might just be ok:

replace(replace(replace(replace(replace($mycol$,
    "ABC", "DEF"),
    "XYZ", "123"),
    "...", "..."),
    "...", "..."),
    "...", "...")

Otherwise, you can probably solve this using a Column List Loop Start node to loop through your columns and then apply the Java Snippet. You will need to play around with flow variables a bit to make it work, though.
Kind regards,
Alexander

Hi Alexander,

wonderful. It works. Thanks a lot for your fast answers. I will try the Column List Loop Start possibility later, too. But this day was a good day. :grinning:
Kind regards,
Ol

2 Likes

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