Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Java string cut help...

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java string cut help...

    Hi all, was hoping to get some help with this.

    If I am given a string such as:
    REG20140509-0001

    I want to cut out the year/month/day, so in this case '20140509'.
    I know that the characters in front of the date will always be letters, not numbers, but I don't know how many. Three in the above example, but could be more or less.
    I do know that the date will always be followed by a dash and four digits.

    Thanks for any help!!!!!!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java string cut help...

    One idea:
    find the "-" and backup x characters over the date and use the substring method.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java string cut help...

    Quote Originally Posted by Norm View Post
    One idea:
    find the "-" and backup x characters over the date and use the substring method.
    Thanks Norm. I've used the substring method before, but not sure how to get it to work right to left?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java string cut help...

    how to get it to work right to left?
    It doesn't directly.
    The starting index is computed from the location of the "-" by subtraction.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    May 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java string cut help...

    Hi!
    You can first split the string by '-' character, so you will have "REG20140509" and store into a String variable (let's say date).
    After that you can apply a regexp over this string. Use "\\w" as a pattern.
    Then you will extract a substring from date variable. (ex: date.substring(matcher.end()+1); ) and you will get "20140509".

    Hope this helps.

    --- Update ---

    Sorry, I made a mistake.
    Use "\\d" pattern. And after that date.substring(matcher.start()) will return "20140509"

Similar Threads

  1. String being 'cut off in for loop
    By Watto in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 22nd, 2013, 12:59 PM
  2. HttpURLConnection doesnt quite cut it
    By chopficaro in forum Java Theory & Questions
    Replies: 2
    Last Post: August 23rd, 2012, 08:48 AM
  3. Re: Problem with BufferedReader : readLine() cut the line
    By caveden in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: August 10th, 2012, 08:46 AM
  4. Can't get lines to cut at 60 characters and continue on next line right
    By JavaN00b in forum What's Wrong With My Code?
    Replies: 10
    Last Post: May 22nd, 2011, 09:44 AM