Java strings

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fungi
    New Member
    • Apr 2007
    • 3

    Java strings

    I have a question:
    for (int i = text.length()-1; i>=0; i--)
    {
    //some code
    }

    What does -1 mean?
    I know what these--> i>=0; i-- mean but I do not understand what -1 means?

    Thanks
  • nmadct
    Recognized Expert New Member
    • Jan 2007
    • 83

    #2
    Originally posted by fungi
    I have a question:
    for (int i = text.length()-1; i>=0; i--)
    {
    //some code
    }

    What does -1 mean?
    I know what these--> i>=0; i-- mean but I do not understand what -1 means?

    Thanks
    When you're counting the character positions in a string, you start counting at zero. That means that the index of the last character is 1 less than the length of the string. That's why it was necessary to subtract 1 from the length to get the correct starting point.

    Comment

    • fungi
      New Member
      • Apr 2007
      • 3

      #3
      Originally posted by nmadct
      When you're counting the character positions in a string, you start counting at zero. That means that the index of the last character is 1 less than the length of the string. That's why it was necessary to subtract 1 from the length to get the correct starting point.
      Thanks a lot
      Not knowing this info prevented me from finishing a code that I had to write

      Comment

      • nmadct
        Recognized Expert New Member
        • Jan 2007
        • 83

        #4
        Originally posted by fungi
        Thanks a lot
        Not knowing this info prevented me from finishing a code that I had to write
        I'm glad I could help!

        Comment

        Working...