String Leading Char(s) Removal

I have a string custom parameter like XXXXX-YYY where the XXXXX is always 5-digits, leading digits are 0 if not used and YYY are characters. I would like to strip off any leading 0s.

For instance: 22106-ABC → 22106-ABC, 01574-ABC →1574-ABC, 00248-ABC → 248-ABC etc

What’s a “nice” way to do this?

regex: ^0*(\S*)
that will return everything other than leading 0's

How do I use that in the expression language?

Would using right() do the job?

1 Like

Combination of right() len() and search() might do it. There could be extra chars to the right, but the first is always 5 digits

The easiest way that comes to mind when I think of it that way may be to just do int(left()) for the first 5, let Ignition do the stripping, and then concat with the remaining string using right() and len()-5

int(left({tag}, 5)) + right({tag}, len({tag}) - 5)

Of course, there are more ways to skin the cat, but I like simple and readable solutions, which typically does not include regex.

try split(sourceString, "^[0|\\D]*")?

Edit: this fails if a letter is the first thing after your leading zeros.

You can use substring() instead of right() to avoid needing len().

You cannot add an integer to a string, though. I recommend:

stringFormat("%d%s", toInt(left({tag})), substring({tag}, 5))