at the moment I’m trying to use the function “replace” in a "Java Snippet "/ “Java Snippet (simple)” node for the replacement of strings in a column. The strings start with “3RT2…” and I want to rename these to “3RTx…”. The code e.g. in a “Java Snippet (simple)” is as following:
String ProdMLFB =$Product MLFB$; //reading string in a column
ProdMLFB.replace(“3RT2”, “3RTx”); //replace string
return ProdMLFB; //write new string in a new column
But the new column “Test” always shows the old string starting with “3RT2”.
What’s wrong? The “Java Snippet (simple)” node works in the workflow without error message!
A “Java Snippet” shows the same behaviour!!
Using the replace function in a “String Manipulation” node works fine!
String ProdMLFB = $Product MLFB$;
String NewString = ProdMLFB.replace(“3RT2”, “3RTx”);
I’m not a Java expert, but it seems that ProdMLFB value doesn’t change its content with replace method and a new value has to be initiated and returned
Hi @Martin_K, that’s absolutely right. Strings in Java are immutable, i.e. Cannot be modified, so methods like replace have to generate a new string rather than modify the existing one.
thanks @Martin_K and @s.roughley for quick response!
After changing the code as described above from Martin_K, the “Java Snippet” / “Java Snippet (simple)” works fine.
I’m not a java expert too! Searching for a solution in the net, I could’t find the right reference.
I’m used to write code in VBA, seems java is a little different:grinning:!
Why don’t you use the String Manipulation node? It’s easier to use and in the workflow it’s more clear what’s happening.
Hi @thor ,
I fully agree with you, but my usecase of replacing parts of strings is more complexier than shown in my example above. Using the “String Manipulation node” means, that I have to put more then ten nodes in series in my workflow.