Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2008

Tip: Looking for answers? Try searching our database.

Split a price String Object into dollars and cents String Objects

Thread view: 
Sisilla - 11 Mar 2008 18:46 GMT
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
Sisilla - 11 Mar 2008 18:52 GMT
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 "/."
Sisilla - 11 Mar 2008 18:55 GMT
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 "/."
Sisilla - 11 Mar 2008 18:59 GMT
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.
Roedy Green - 12 Mar 2008 01:59 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.