77

Code:

public class Test {
    public static void main(String[] args) {
        String str = "University";
        System.out.println(str.substring(4, 7));
    }   
}

Output: ers

I do not really understand how the substring method works. Does the index start at 0? If I start with 0, e is at index 4 but char i is at 7 so the output would be ersi.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
DarkLeafyGreen
  • 69,338
  • 131
  • 383
  • 601
  • Also, If you put the begin index as str.length(), It won't throw an IndexOutofBounds excetion. It'll just return an empty string. – आनंद Oct 13 '16 at 04:29

9 Answers9

147

0: U

1: n

2: i

3: v

4: e

5: r

6: s

7: i

8: t

9: y

Start index is inclusive

End index is exclusive

Javadoc link

Axel Fontaine
  • 34,542
  • 16
  • 106
  • 137
  • 2
    thanks, thats exactly what I found informative; beginIndex - the beginning index, inclusive. endIndex - the ending index, exclusive. – DarkLeafyGreen Dec 31 '10 at 12:22
  • Just think of it as a char array with base 0 as all other arrays, and that the second argument is the *stop* position rather than the end position. – Henrik Erlandsson Aug 30 '13 at 13:59
14

Both are 0-based, but the start is inclusive and the end is exclusive. This ensures the resulting string is of length start - end.

To make life easier for substring operation, imagine that characters are between indexes.

0 1 2 3 4 5 6 7 8 9 10  <- available indexes for substring 
 u n i v E R S i t y
        ↑     ↑
      start  end --> range of "E R S"

Quoting the docs:

The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
moinudin
  • 134,091
  • 45
  • 190
  • 216
8

See the javadoc. It's an inclusive index for the first argument and exclusive for the second.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Matt
  • 8,367
  • 4
  • 31
  • 61
4

Like you I didn't find it came naturally. I normally still have to remind myself that

  • the length of the returned string is

    lastIndex - firstIndex

  • that you can use the length of the string as the lastIndex even though there is no character there and trying to reference it would throw an Exception

so

"University".substring(6, 10)

returns the 4-character string "sity" even though there is no character at position 10.

peter.murray.rust
  • 37,407
  • 44
  • 153
  • 217
3
public String substring(int beginIndex, int endIndex)

beginIndex—the begin index, inclusive.

endIndex—the end index, exclusive.

Example:

public class Test {

    public static void main(String args[]) {
        String Str = new String("Hello World");

        System.out.println(Str.substring(3, 8));
    }
 }

Output: "lo Wo"

From 3 to 7 index.

Also there is another kind of substring() method:

public String substring(int beginIndex)

beginIndex—the begin index, inclusive. Returns a sub string starting from beginIndex to the end of the main String.

Example:

public class Test {

    public static void main(String args[]) {
        String Str = new String("Hello World");

        System.out.println(Str.substring(3));
    }
}

Output: "lo World"

From 3 to the last index.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ratul Bin Tazul
  • 2,121
  • 1
  • 15
  • 23
2

Yes, the index starts at zero (0). The two arguments are startIndex and endIndex, where per the documentation:

The substring begins at the specified beginIndex and extends to the character at index endIndex - 1.

See here for more information.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Richard H
  • 38,037
  • 37
  • 111
  • 138
1

The substring starts at, and includes the character at the location of the first number given and goes to, but does not include the character at the last number given.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Brett H
  • 21
  • 2
0

For substring(startIndex, endIndex), startIndex is inclusive and endIndex are exclusive. The startIndex and endIndex are very confusing. I would understand substring(startIndex, length) to remember that.

elliptic00
  • 427
  • 4
  • 13
0
public class SubstringExample
{
    public static void main(String[] args) 
    {
        String str="OOPs is a programming paradigm...";

        System.out.println(" Length is: " + str.length());

        System.out.println(" Substring is: " + str.substring(10, 30));
    }
}

Output:

length is: 31

Substring is: programming paradigm
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131