Best answer

Remove last 3 digits from a string

  • 25 August 2022
  • 4 replies
  • 618 views

Userlevel 1

I’m setting up a logic sequence that takes any UK postcode and removes the final 3 characters and returns the remaining characters.

e.g.

B48OP becomes B4

B755UG becomes B75

and WV125ET becomes WV12

The postcode string could be 5, 6 or 7 characters long - in each case I want to remove the final 3 characters and return the remaining characters as above. I’ve already created steps to remove spaces and make upper case - it’s just this final bit I can’t figure out.

Any ideas?

icon

Best answer by Troy Tessalone 25 August 2022, 17:27

View original

This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

4 replies

Userlevel 7
Badge +14

Hi @Hyde 

Good question.

Check out this related topic:

https://community.zapier.com/featured-articles-65/slice-text-with-javascript-code-14510

Userlevel 1

Hi Troy, thanks for this!

Ok, so I’ve followed that and used the following code to slice off the final 3 characters from the testing value “PR26QY”

 

let Text = inputData.Text;

let Text1 = Text.slice(-3);

output = [{Text1}];

 

But then it’s the final 3 characters “6QY” that are returned in the output and not the initial string which I need.

Can I use some code to remove the above output from the initial string to leave the remaining characters? Something like the following:

Output = [{Text} minus {Text1}]

?

Userlevel 7
Badge +14

@Hyde 

Try .slice(0,-3);

Userlevel 1

That’s worked perfectly, thanks again for the help!