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 16 of 16

Thread: substring with negative endIndex

  1. #1
    Member
    Join Date
    Feb 2011
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default substring with negative endIndex

    Ultimately what I need is the PHP version of substring. I have a string that looks like this:

    12345:1

    It could also be:
    1234567:2

    Meaning the number before the : could be of any length. The part after the : will always be 1 in length.

    So what I want to do is this:

    str.substring(0,-2);

    Is there an easier way to do this because I can't seem to find that specific functionality.


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

    Default Re: substring with negative endIndex

    Time to read the API doc for the String class.
    Take a piece of paper and write down the characters in the string and then mark off where you want the start of the substring to be and where you want the end to be. Then look at the the relationships between those marks and the length of the string and what ever else is available.

  3. #3
    Member
    Join Date
    Feb 2011
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: substring with negative endIndex

    Wow. Thanks for the attitude. And thanks for not answering my question. I know how the substring method works. But the doc says nothing about using negative indexes.

    All I was looking for was a simple "you're going to need to search for the colon and use that as the endIndex" or "here is a better way to do this".

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

    Default Re: substring with negative endIndex

    My idea is to help you to learn how to program in java. Part of that is learning how to read the API doc.
    I don't give out much code. That is up to you to generate. I try to give you ideas of where to look and how to solve the problem.

    But the doc says nothing about using negative indexes.
    If its not there, then you can't do it that way.
    Now take your piece of paper and draw the marks and figure out the indexes.
    I've done it dozens of times. You need to learn how to do it if you want to be a programmer.

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: substring with negative endIndex

    Thanks for the attitude. And thanks for not answering my question. I know how the substring method works. But the doc says nothing about using negative indexes.
    Let us please understand that the format of online forums can sometimes lead to things being misunderstood or taken out of context. What Norm was suggesting is to use one or any combination of the methods documented in the API. I personally don't understand what you need to do, in other words you provided an example of a starting point, but what do you want to pull out from that string? Norm pointed you to the String API, which should give you a starting point for the methods available to accomplish your task.

  6. #6
    Member
    Join Date
    Feb 2011
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: substring with negative endIndex

    I've been programming for nearly a decade, just not in Java. So I understand the basic constructs of programming. I just find the functionality of Java to be a bit lacking, like this in particular.

    This is my second interaction with Norm and both have been very condescending.

    Regardless, I just want everything before the colon in the string. And I'm guessing I will just do this:

    sub = str.substring(0, str.indexOf(":"));

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: substring with negative endIndex

    Try it and see what happens.
    That'll give you the correct answer a lot quicker then waiting for someone else to copy to a source, compile it and execute it.
    If there are errors, or the output is not what you want, post the results here and ask you questions.

  8. #8
    Member
    Join Date
    Feb 2011
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: substring with negative endIndex

    Seems to have done the trick. I just had a lot of code to write around that and I figured if someone had the answer here quickly it would save me some time.

    Sorry to have bothered you so much.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: substring with negative endIndex

    99% of the posters here are students, some are just looking for a handout. Many want to learn and I try to help them find the answer and give them techniques they can use to find answers. I usually wait a few posts before giving out code. My experience is that you learn by trying.
    For a simple question about how to use the substring() method, I figured it must be another student too lazy to read the API doc and just wanting someone to give him the answer.
    Last edited by Norm; July 18th, 2011 at 03:24 PM.

  10. The Following User Says Thank You to Norm For This Useful Post:

    ober0330 (July 18th, 2011)

  11. #10
    Member
    Join Date
    Feb 2011
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: substring with negative endIndex

    Well I can appreciate that tactic, I assure you. I spent far too many years on the PHP boards dealing with people like that. I'm sorry if I came off that way. And if I'm being completely honest, I find the Java Docs hard to navigate and search through, but maybe that's because I'm so used to the PHP Docs. I just wanted to make sure I wasn't missing something obvious in the documentation.

  12. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: substring with negative endIndex

    Have you ever tried to get thru MS's C and C++ doc?
    I find java doc so soothing after I've tried to find something in the Visual Studio's help.

  13. #12
    Member
    Join Date
    Feb 2011
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: substring with negative endIndex

    I was a VB6 programmer at my first job and I found it to be frustrating at least. I'm sure their C and C++ docs aren't much better. The C code I've written was never done in VS so I didn't depend on MS for my information. So yeah, I would agree that Java docs are probably a step up but I just wish someone would take them and format them and make them easier to search.

  14. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: substring with negative endIndex

    make them easier to search.
    Do you know about the Index? Its a link at the topic of the API doc pages in the blue bar. http://download.oracle.com/docs/cd/E...se/6/docs/api/
    There is an alphabetical(A-Z) list with links.
    Also for the tutorial: The Really Big Index

    BTW: One of my java projects was a Search that will go thru the API doc I have on my computer and return a page of links to the pages with the searched for text.

  15. #14
    Member
    Join Date
    Feb 2011
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: substring with negative endIndex

    Interesting. I will look into that stuff more. I also have some books I am trying to use.

    Thank you for your help. I really do appreciate it.

  16. #15
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Thumbs up Re: substring with negative endIndex

    Quote Originally Posted by ober0330 View Post
    Wow. Thanks for the attitude. And thanks for not answering my question.
    There is no attitude here, especially from Norm. He is one of our most valued members and helps countless people every day.

    I know how things can be taken in the wrong way when you read them. I'm sure you appreciate this.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  17. #16
    Member
    Join Date
    Feb 2011
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: substring with negative endIndex

    Well since I explained that I'm not a n00b, things have turned 180. And maybe I was reading the wrong way before.

Similar Threads

  1. [SOLVED] Is it possible to get factorial of negative number
    By Lokesh in forum Java Theory & Questions
    Replies: 3
    Last Post: August 4th, 2011, 05:45 PM
  2. [SOLVED] Keeping Negative Fractions
    By Kerrigan in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 10th, 2011, 05:03 PM
  3. Negative
    By xew123 in forum Java Theory & Questions
    Replies: 1
    Last Post: March 6th, 2011, 07:25 PM
  4. Need this to end when a negative number is entered
    By ponyexpress in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 28th, 2010, 09:02 AM
  5. Evaluating to negative zero
    By helloworld922 in forum Java Theory & Questions
    Replies: 6
    Last Post: June 25th, 2009, 02:34 PM