Remove string with variable length after certain character

Hi there, can anyone please help me in resolving this issue?

I need to remove the string & variable extension in a number column. Example:

123456DUP10
123456DUP2
123456DUP

Result is expected as:

123456
123456
123456

Which String Manipulation command should I use?

Thank you!
Xenia

Hi @xeniaay

Welcome to the KNIME forum Go for the the String Manipulation Node and use:
substr($column1$,0,indexOfChars($column1$,“DUP” ) )

gr. Hans

2 Likes

Hi @HansS,

Thank you so much for your prompt response. It works!

Best regards,

Xenia

2 Likes

Hi @HansS

Sorry… I overlooked one error… This expression also produce an incorrect result for:

Example:
CD123456DUP
CD123456

Result:
C
C

Is there a way to fix the above?

Thanks!
Xenia

Hi Xenia,

no problem: another solution is to use the String Manipulation node and do:
removeChars($column1$,“CDUP” )
This removes as C-D-U-P characters from your string.
Happy KNIME-ing.
gr. Hans

Hi @HansS

Yes, this should work to remove “DUP” at the end of the string but it doesn’t remove any possible extension # after DUP. What I want is:

Example:
CD123456DUP
CD123456DUP10
CD123456DUP 4
CD123456

Expected result:
CD123456
CD123456
CD123456

Can we make a twist to substr($column1$,0,indexOfChars($column1$,“DUP” ) )?

Thank you!

Xenia

Hi there @xeniaay,

welcome to KNIME Community!

So if you always want first 8 characters from your column you can use

substr ($column_name$, 0, 8)

and if you want everything before “DUP” string then I would use this function

substr ($column_name$, 0, indexOf ($column_name$, “DUP”) ).

In latter case if there is no “DUP” you will get empty string. Then you can use Rule Engine node to replace empty string with original value.

Hope this helps!

Br,
Ivan

3 Likes

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