Results 1 to 4 of 4

Thread: Split a string

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

    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
    Northern Virginia, United States
    Rep Power

    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.

    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
    Voorschoten, the Netherlands
    Blog Entries
    Rep Power

    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);
       /* not a mm:ss or mm,ss string */
    kind regards,

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

  4. #4
    Join Date
    Aug 2015
    Northern California
    Rep Power

    Default Re: Split a string

    This video will be very enlightening for you and it is short.
    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