Results 1 to 5 of 5
  1. #1
    mutagen is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default Extracting Double from String?

    Alright guys let's say I have a String:

    "\u00A3 3.39 per Unit"

    the String displays cost of a product and what I need to do next is get the "3.39" part of the String and continue working with that. How can I extract that number? I was thinking about using a loop with charAt(); Method and just parse it to double but it would extract 003 in front of that String as well since it's a code I must use to get GBP symbol. so does anyone have an elegant solution to this?


    should I use loop with charAt();
    substring(3);
    and then parse?

    or does anyone have any other solutions?

    TYVM!
    Last edited by mutagen; 03-19-2011 at 05:41 PM. Reason: Complete.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default

    Might be overkill for something like this, but I'll post anyways:
    Lesson: Regular Expressions (The Java™ Tutorials > Essential Classes)

    If you expect the strings to remain constant, just split on whitespace or use a StringTokenizer, take the string with the number and use Double.parseDouble to get the double value

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    And if you do need to use regex, there's another, more detailed tutorial on this site:
    Regular-Expressions.info - Regex Tutorial, Examples and Reference - Regexp Patterns

    db

  4. #4
    mutagen is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default

    Thanks guys!

  5. #5
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    if you know your string is going to be that kind of structure, no need to use much regular expression. Do a split() on white space (\s+) , then get the 2nd element (index 1 from your example) from the returned array.

Similar Threads

  1. String to double HELP please!
    By zhen1337 in forum New To Java
    Replies: 33
    Last Post: 02-08-2011, 09:30 AM
  2. String to double errors
    By bigvanilla in forum New To Java
    Replies: 9
    Last Post: 12-05-2010, 08:42 AM
  3. Problem extracting items from a string with separators
    By PepsiColaMola in forum New To Java
    Replies: 3
    Last Post: 04-24-2009, 08:30 PM
  4. Replies: 4
    Last Post: 07-07-2008, 01:32 PM
  5. program help: Extracting words from a string
    By toad in forum New To Java
    Replies: 1
    Last Post: 11-04-2007, 06:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •