Delete last Character of String if it's "0"

Hello all,

I am trying to figure out if there is a way in KNIME to check if the last character in a string is 0 and delete it, and if it is not, the last character should stay as it is. For example:

Value 1: 20 → 0 at the end should be deleted (new Value: 2)
Value 2: 18 → both number/characters stay as it doen’t end with a 0

I would be grateful if someone can help me with this issue.

Regards

Hi,

you can use regexReplace in a String Manipulation Node:

regexReplace($yourString$, "0$", "")
regexReplace($yourString$, "(.)0$", "$1")

“0$” matches a zero at the end of a string
“(.)0$” uses substitution, isn’t actually needed here but I wanted to show off :wink:

PS: Since Gordon wants to do math: :wink:
if(mod($yourInt$, 10) == 0, $yourInt$/10, $yourInt$)

5 Likes

Hello @hm1995
You can achieve this task with:

  1. ‘Math Formula’ node rounding to the desired decimal (floor in this case)
    floor($value$)
  2. ‘Math Formula’ calculating the differences
  3. Rule Engine:
    $differences$ = 0 => "TRUE"
    TRUE => "FALSE"
  4. After that you can use one of your favorite splitters. i.e. Up == True / Down == False
  5. Math Formula in the Upper
    $value$ + 2
  6. Concatenate back the both branches of the Split

BR

PS. After writing this I realized ‘last character in string’, then take @Thyme 's approach. This post can stay, as if someone else looking for the same challenge with numeric format

3 Likes

Thanks for your reply. Works perfectly fine :slight_smile:

In case someone looking for another solution, I solved it with Java Snippet (simple) node:

String str = $yourString$;

if (str.endsWith(“0”)){
str = str.substring(0, str.length() - 1);
return str;
} else {
return str;
}

3 Likes

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