Results 1 to 4 of 4

Thread: Split a string

  1. #1
    glockner is offline Member
    Join Date
    Jan 2015
    Posts
    31
    Rep Power
    0

    Default Split a string

    I am working on a countdown timer and am trying to figure out how to split a string so it will read hours, minutes, and seconds. The format I want is for the numbers to be split by ":" and ",". So if the user enters 15:30 or 15,30 it would store as 15 minutes and 30 seconds. If the user enters nothing it would be set to 0. I have instance variables for hours, minutes and seconds. So, my question is how do I use the split command correctly in this instance?

    Thank you
    Last edited by glockner; 09-03-2015 at 10:36 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Split a string

    Check out the split command of the String class. You will need to use the regular expression "[:,]" as the split delimiter. You should also read about regex patterns in the Pattern class. It is pretty certain you will encounter them in the future.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Split a string

    Or use the simple indexOf( ... ) method:

    Java Code:
    int p= string.indexOf(',');
    if (p < 0) p=string.indexOf(':');
    if (p >= 0) {
       String mins= string.substring(0, p);
       String secs= string.substring(p+1);
    }
    else
       /* not a mm:ss or mm,ss string */
    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  4. #4
    Join Date
    Aug 2015
    Location
    Northern California
    Posts
    31
    Rep Power
    0

    Default Re: Split a string

    This video will be very enlightening for you and it is short.
    https://www.youtube.com/watch?v=RxeD...C9B19&index=13
    Everything great is a challenge and all the greats were challenged at one time just like us.

Similar Threads

  1. Split string
    By codeAJ in forum New To Java
    Replies: 14
    Last Post: 05-08-2011, 10:51 AM
  2. String split help
    By MiddleBlocker in forum New To Java
    Replies: 4
    Last Post: 03-11-2011, 02:57 AM
  3. Split a String with split()--Help
    By danilson in forum New To Java
    Replies: 7
    Last Post: 11-19-2010, 04:08 PM
  4. How to split a String using split function
    By Java Tip in forum java.lang
    Replies: 4
    Last Post: 04-17-2009, 08:27 PM
  5. How to split a String using split function
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 09:32 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
  •