0

If my String is like this:

var dollarValue = "$7.50;$15;50%";

What would be the best method of extracting each value? Each semicolon is used as a separator between the part of the string that matters. So like:

$7.50

$15

50%

I understand how to extract two values with only one semicolon, but I'm not sure how I would continue on down the string using each subsequent semicolon as the next starting point...

2 Answers 2

4

How 'bout String.split()?

var arrayOfValues = dollarValue.split(";"); // ["$7.50", "$15", "50%"]
Sign up to request clarification or add additional context in comments.

4 Comments

Does split just take the ";" and enter it into an array of it's own? I would then extract the information out of that newly created array, right?
@dcolumbus: arrayOfValues[0] will be "$7.50", arrayOfValues[1] will be "$15", etc. just like arrays work.
It will basically cut the string up on every semicolon, returning an array of the parts.
Perfect. It's these little things that I forget over time when I'm not constantly using it. Learning too many languages scrambles my brain.
1

You can use the split method:

var values = dollarValue.split(";");

This will return an array with the values between the semicolons.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.