Remove all characters from string from certain point

Hi,

I’m trying to remove the prices from a list of items in a dropdown. For example…

1-Day Experience-$5.00
3-Day Experience-$10.00
1 Month Experince-$100.00

So I want to remove everything from “-$”. Searching brought me to the slice command-but I can’t get that to work consistently as the number of characters in the price varies from product to product. I’ve seen discussions using replace and substrings and it confuses me!

Help appreciated.

Cheers

Actually, got slice working by setting conditions for string containing specific prices. Not very elegant-and, thankfully, only six different products/prices to account for-but it works!

Would still be interested in a cleaner solution for learning purposes.

Cheers

Here is a quick demo based on my understanding of your need. These slice statements will return everything in the string from the beginning to “-$” --so it assumes your prices are always at the end of the string: [[ Target.text.slice(0, Target.text.indexOf('-$')) ]] If they are in the middle of a string, you can combine several slice() functions and/or substring() functions, or chain them in multiple actions if that helps.

Yes, in general the javascript string routines can be confusing. To learn more, I usually Google a function name, like “slice() javascript” or what I’m trying to do, like “strip the last character from a string javascript” --basically append “javascript” to what I’m trying to do, since the built-in functions in Axure are based on javascript.

slice text to remove prices.rp (54.3 KB)

Thanks-really helpful, as ever. I did actually try the indexOf method, as discovered in a couple of other threads-but it didn’t do anything. I might have had a typo in there somewhere. Is that removing “-$” as well, or only everything after it?

Perhaps you can guess my next question… How would I also keep ONLY the prices-so everything from $…? Is it swapping the order of the array to [[ Target.text.slice(Target.text.indexOf(’$’), 0) ]]…?

I’ll give it a go…

Nope! Apparently not.

Close though… It should just be [[ Target.text.slice(Target.text.indexOf(’$’)) ]]

To demonstrate this, I updated my .rp file to show what the prices are when they get removed, and added a button to restore the prices.
slice text to remove prices.rp (57.5 KB)

The way string slice() works is there are two arguments, a beginning position to start the slice, and an ending position to stop the slice. The first is required, the second is optional. If there is no second argument then the end of the string is used–so the returned slice will contain everything up to and including the last character in the string. Keep in mind strings start at 0 (zero), so in your username, “BNARob” the position (or in javascript lingo, the index ) of ‘B’ is 0.

  • If I wanted to get only the first 3 chars from your username I would use string.slice(0, 3)
  • If I wanted to omit the first 3 chars, I would use string.slice(3)
  • If I wanted the middle 2 chars, I would use string.slice(2, 4)

Another cool thing about slice() is it can slice from the end of the string, working its way backward through the string. To do this, just use negative values in the arguments.

  • If I wanted the last 3 chars (and I don’t know or care how long the whole string might be) then I would use string.slice(-3)

You can find out more details here:

1 Like

Thanks for your help again. I couldn’t get the slice function to work: it was just returning the whole expression, including brackets, as the text output. I got it sorted with a substr and indexOf combo though.

[[ LVAR1.substr( LVAR1.indexOf(’$’)+1) ]]

I added +1 because I realised I needed to remove the dollar sign as well in order to just have the number value.