Results 1 to 7 of 7
  1. #1
    gaborn415 is offline Member
    Join Date
    Feb 2011
    Posts
    25
    Rep Power
    0

    Default Manipulating strings

    I would assume this would be very easy but I'm not seeing it listed anywhere. How can I change a string variable to read something else? For instance I want to take this string "$200,000" and convert it to "200000". How can I do this in Java? Thanks for the help!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You need to give us the specific rules of your desired transformation. One example isn't enough to be able to guess your rules.

  3. #3
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default

    subString is going to probably do what you are looking for. You can define a beginning and end index(if needed) and 'chop' the string down to what you want. You can also use indexOf and pass in a token if you want to manipulate your string based on the presence/position of some character or pattern

  4. #4
    gaborn415 is offline Member
    Join Date
    Feb 2011
    Posts
    25
    Rep Power
    0

    Default

    I have three variables which are being passed to an array. I want them to become doubles and so I need to get rid of the pieces of the string which cannot convert to a double. So I have "$200,000" and "5.75%" and "7 years" as strings. I want to convert them to doubles to do math with them. So I want the strings to become these doubles 200000, and 5.75 and 7. Hope that helps!

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

    Default

    you can use replaceAll() or replace().

    Java Code:
    String myString = "$200,000";
    System.out.println ( myString.replaceAll( "[$,]", ""  );
    check the documentation for replaceAll(). I write only from memory.

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

    Default

    So you want to retain all characters that are digits, and exclude the rest. Read the documentation for String#replaceAll(...) as hinted by JavaHater, along with the Pattern API.

    Some valuable lessons on using regex can be found here:
    Regular-Expressions.info - Regex Tutorial, Examples and Reference - Regexp Patterns

    db
    Last edited by DarrylBurke; 04-03-2011 at 07:26 AM.

  7. #7
    gaborn415 is offline Member
    Join Date
    Feb 2011
    Posts
    25
    Rep Power
    0

    Default

    Thanks for the help, sorry it took me a while to respond! ;)

Similar Threads

  1. Picture manipulating help
    By evolutiongsr8 in forum New To Java
    Replies: 0
    Last Post: 03-26-2011, 06:47 AM
  2. Manipulating BufferedImage
    By ace_quorthon in forum Java 2D
    Replies: 1
    Last Post: 01-08-2011, 04:35 PM
  3. Replies: 1
    Last Post: 11-04-2010, 02:42 PM
  4. Manipulating URLs
    By TheFlying_Boy in forum Networking
    Replies: 0
    Last Post: 08-03-2009, 05:01 PM
  5. Manipulating XML
    By JosephMConcepcion in forum XML
    Replies: 2
    Last Post: 04-26-2009, 12:01 AM

Posting Permissions

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