|
|
Split a price String Object into dollars and cents String Objects
Hello All,
I am trying to split a String object below -:
String price = "150.0"
String priceArr[] = price.split(".", 2);
String dollars = priceArr[0];
String cents = priceArr[1];
Why is it that 'dollars' is assigned "" and 'cents' is assigned
"50.0"? I would like 'dollars' to be assigned "150" and 'cents' to be
assigned "0". What am I doing wrong here? I appreciate any effort to
help me. Thank you for your time and consideration.
Sincerely,
Sisilla
|
|
0
|
|
|
|
Reply
|
Sisilla
|
3/11/2008 5:46:44 PM |
|
Here is the solution if anyone is curious:
The period/dot is a special character in Regular Expressions and has
the meaning "any character". If you want to split around the dot, you
need to use the expression "/."
|
|
0
|
|
|
|
Reply
|
Sisilla
|
3/11/2008 5:52:49 PM
|
|
Oops. That should be "\."
> The period/dot is a special character in Regular Expressions and has
> the meaning "any character". If you want to split around the dot, you
> need to use the expression "/."
|
|
0
|
|
|
|
Reply
|
Sisilla
|
3/11/2008 5:55:42 PM
|
|
Actually you need "\\."
By escaping the dot a reference to the dot is created, as opposed to
the 'any character' meaning; the backslask then needs to be repeated
because Java will strip a backslash before passing the expression to
the Regex engine.
|
|
0
|
|
|
|
Reply
|
Sisilla
|
3/11/2008 5:59:05 PM
|
|
On Tue, 11 Mar 2008 10:46:44 -0700 (PDT), Sisilla
<sisilla14@gmail.com> wrote, quoted or indirectly quoted someone who
said :
>String priceArr[] = price.split(".", 2);
you mean literal ., . is a magic regex command meaning any char.
You must "quote" it. See http://mindprod.com/jgloss/regex.html
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
|
|
0
|
|
|
|
Reply
|
Roedy
|
3/12/2008 12:59:39 AM
|
|
|
4 Replies
437 Views
(page loaded in 0.432 seconds)
|
|
|
|
|
|
|
|
|