Sidebar

How do I remove a three character suffix off a string?

0 votes
186 views
asked Oct 6, 2023 by mike-r-7535 (13,830 points)
I have an identifier that has a three charater value on the end.  How do I remove that suffix?

1 Answer

0 votes
 
Best answer

Using StringUtils, you can take a substring from the the beginning of the string to just before the third to last character.  A negative index means count back from the end of the String by this many characters.

var idWithSuffix = "123456789098765432_BB";

// This will take a substring from the beginning (index 0), up to the third character from the end of the string.
var idOnly = StringUtils.substring(idWithSuffix, 0, -3);
qie.warn("idOnly = " + idOnly);

// This will take a substring from the third character from the end of the string to the end.
var suffixOnly = StringUtils.substring(idWithSuffix, -3);
qie.warn("suffixOnly = " + suffixOnly);

This prints the following:

idOnly = 123456789098765432
suffixOnly = _BB 

answered Oct 6, 2023 by mike-r-7535 (13,830 points)
...