substring index range

JavaStringSubstring

Java Problem Overview


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.

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

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.

Solution 3 - Java

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

Solution 4 - Java

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.

Solution 5 - Java

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.

Solution 6 - Java

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.

Solution 7 - Java

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.

Solution 8 - Java

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.

Solution 9 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionUpCatView Question on Stackoverflow
Solution 1 - JavaAxel FontaineView Answer on Stackoverflow
Solution 2 - JavamoinudinView Answer on Stackoverflow
Solution 3 - JavaMattView Answer on Stackoverflow
Solution 4 - Javapeter.murray.rustView Answer on Stackoverflow
Solution 5 - JavaRatul Bin TazulView Answer on Stackoverflow
Solution 6 - JavaRichard HView Answer on Stackoverflow
Solution 7 - JavaBrett HView Answer on Stackoverflow
Solution 8 - Javaelliptic00View Answer on Stackoverflow
Solution 9 - JavaPrince GeraView Answer on Stackoverflow