Results 1 to 7 of 7
  1. #1
    ravian is offline Senior Member
    Join Date
    Nov 2007
    Posts
    115
    Rep Power
    0

    Default String substring function

    Hi,

    I am using the substring methodto get part of a string.

    Java Code:
    String string = "WelcomeToJava";
    System.out.println(string.substring(9,12));
    Output:
    Java Code:
    Jav
    I want to get output "Java". The index of "J" is 9 and index of a is 12. It appears that the first argument to the method is inclusive and the second one is exclusive. Please correct me.

  2. #2
    simi's Avatar
    simi is offline Member
    Join Date
    Dec 2007
    Location
    Singapore
    Posts
    18
    Rep Power
    0

    Default

    String string = "WelcomeToJava";
    System.out.println(string.substring(9,13));

  3. #3
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    9

    Default

    I hope java docs on substring method of String class should make you more clear on this.
    dont worry newbie, we got you covered.

  4. #4
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    9

    Default

    Hello, ravian.

    The substring method is a funny one. The way that I understand is that the first parameter has zero-based indexing and the second has one-based indexing. For example:

    first parameter:____00 01 02 03 04 05 06 07 08 09 10 11 12
    second parameter:___01 02 03 04 05 06 07 08 09 10 11 12 13
    string:_____________W__e__l__c__o__m__e__T__o__J__a__v__a
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  5. #5
    ravian is offline Senior Member
    Join Date
    Nov 2007
    Posts
    115
    Rep Power
    0

    Default

    This explains. As tim said: the first parameter is zero based and the second one is one based.

    Strange but its like this.

    Thanks everyone.

  6. #6
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    10

    Default

    Quote Originally Posted by tim View Post
    Hello, ravian.

    The substring method is a funny one. The way that I understand is that the first parameter has zero-based indexing and the second has one-based indexing. For example:

    first parameter:____00 01 02 03 04 05 06 07 08 09 10 11 12
    second parameter:___01 02 03 04 05 06 07 08 09 10 11 12 13
    string:_____________W__e__l__c__o__m__e__T__o__J__a__v__a
    Very interesting Tim... I learned something new today, seriously. :)

  7. #7
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    9

    Default

    Thank you CaptainMorgan. ;)
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

Similar Threads

  1. 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
  2. help me with a realy easy program (substring)
    By michcio in forum New To Java
    Replies: 7
    Last Post: 01-27-2008, 01:41 AM
  3. Replies: 0
    Last Post: 11-20-2007, 05:59 PM
  4. substring
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-11-2007, 09:15 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
  •