I hope you can better understand with this example:

Answer from Adrian Adamczyk on Stack Overflow
Discussions

[JAVA] Substring method question about indexing.
Index 0 does refer to R. The range of the substring method is [beginIndex, endIndex). So what you have says to start at the beginning, then include only things before the first character, which is nothing, so you get an empty string. If you want just the first character you need substring(0, 1). It may be easier to think of if you consider the parameters to be beginIndex and beginIndex + substringLength. More on reddit.com
🌐 r/learnprogramming
2
1
April 11, 2013
Why does the substring method in the String class create a new object and return that memory address rather than the information itself or interning it into the String Pool and returning that memory address?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/javahelp
11
9
September 27, 2022
How to correctly use the substring method? (Java)
You are deeply confused about how substring() works - it's a method of String, and it does not take Strings as parameters. See here for an example. More on reddit.com
🌐 r/learnprogramming
5
5
February 2, 2013
[JAVA] Best practice to obtain substring using index of a character that may not be in original string.
Step through the string, one character at a time, looking for a comma. If you reach either the end of the string or a comma, you know what the first id is. More on reddit.com
🌐 r/learnprogramming
7
1
April 10, 2015
🌐
Codecademy
codecademy.com › docs › java › strings › .substring()
Java | Strings | .substring() | Codecademy
April 28, 2025 - Yes, string indexing in Java starts at 0, so the first character of a string is at index 0. When using the substring() method, the beginIndex parameter follows this zero-based indexing.
🌐
GeeksforGeeks
geeksforgeeks.org › java › substring-in-java
Java String substring() Method - GeeksforGeeks
April 11, 2025 - In Java, the substring() method of the String class returns a substring from the given string. This method is most useful when you deal with text manipulation, parsing, or data extraction. This method either takes 1 parameter or 2 parameters, i.e., start and end value as arguments.
🌐
Vultr Docs
docs.vultr.com › java › standard library › java › lang › string › substring()
Java String substring() - Extract Substring | Vultr Docs
January 1, 2025 - The substring() method in Java is a fundamental tool for string manipulation, allowing developers to extract specific parts of a string based on given index positions.
🌐
BeginnersBook
beginnersbook.com › 2013 › 12 › java-string-substring-method-example
Java String substring() Method with examples
substring(0, str.length()-1): This excludes last character as the length() method returns the length of the string and when we provide str.length()-1 as the endIndex, it excludes last character. public class JavaExample{ public static void main(String args[]) { String str= new String("Pasta"); ...
🌐
IONOS
ionos.ca › digital guide › websites › web development › java substrings
How to use the Java substring method - IONOS CA
January 6, 2025 - As mentioned above, there are two ways to use Java’s substring() method. The first only defines the starting point of the substring that you want to extract. It breaks up the string from that index to the last character of the string.
Find elsewhere
🌐
Reddit
reddit.com › r/learnprogramming › [java] substring method question about indexing.
r/learnprogramming on Reddit: [JAVA] Substring method question about indexing.
April 11, 2013 -

Specifically this:

substring(int beginIndex, int endIndex)

Given:

String test = "Reddit"

I had thought index 0 to refer to R, 1 to e, 2 to d, etc.

But, turns out, running code:

String new = test.substring(0,0);

System.out.println(new);

Will return a value of: "". Just "".

That's how I found out index 0 did not refer to "R" of String test.

My question is:

What does index 0 refer to when using the substring method?

🌐
Software Testing Help
softwaretestinghelp.com › home › java › java substring() method – tutorial with examples
Java substring() Method - Tutorial With Examples
April 1, 2025 - Here, we will take an input String ... the substring starting from the zeroth index and ending on the zeroth index. It will be interesting to see how the program responds. As we have the starting and ending indexes as the same, it should return a blank. However, the program compiles successfully in this scenario. It will return blank for all such values where the starting and ending indexes are the same. Be it (0,0) or (1,1) or (2,2) ...
🌐
HappyCoders
happycoders.eu › home › java › java substring() method: how to use it and how it works
Java substring() Method: How to Use It and How It Works
1 month ago - Experienced Java developers familiar with the use of the method can jump directly to the “How Does Substring Work in Java?” section. The String.substring() method returns a substring of the original string, based on a beginning index and an ending index. The best way to explain this is with an image. In the following example, we extract a substring from position 5 to 8 from the string “HappyCoders” (counting starts at 0):
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-string-substring
Master Java Substring Method: Examples, Syntax, and Use Cases | DigitalOcean
February 20, 2025 - Learn how to use the Java substring method. Explore syntax, practical examples, and common errors to handle substrings effectively.
🌐
Programiz
programiz.com › java-programming › library › string › substring
Java String substring() | Programiz
January 31, 2024 - System.out.println(str1.substring(0, ... a substring from the given string. The substring begins with the character at the startIndex and extends to the character at index endIndex - 1 ·...
🌐
Tutorialspoint
tutorialspoint.com › java › lang › string_substring.htm
Java - String substring() Method
The Java String substring() method is used to retrieve a substring of a String object. The substring starts with the character at the given index and continues to the end of the current string.
🌐
TechVidvan
techvidvan.com › tutorials › java-string-substring
Java String substring() - TechVidvan
December 2, 2025 - A portion of a string is returned ... is inclusive, and endIndex is exclusive. For example, the initial index begins at 0 while the end index begins at 1....
🌐
How to do in Java
howtodoinjava.com › home › string › java string substring()
Java String substring() with Examples - HowToDoInJava
January 10, 2023 - Learn to get a substring from from given String in Java between the two indices. Note that String is character array based type and the first character of String is at 0 index.
🌐
Home and Learn
homeandlearn.co.uk › java › substring.html
java for complete beginners - substring
That's because the second number between the round brackets of substring doesn't mean how many characters you want to grab. It means the position in the string that you want to end at. By specifying 2 we're telling Java to end at the character in position 2 of the string. As you can't go from position 6 backwards to position 2 you get an error instead. (NOTE: If you start the count at 0 in the string "Bill", you might think that position 2 is the letter "l".
🌐
Baeldung
baeldung.com › home › java › java string › java string.substring()
Java.String.substring() | Baeldung
April 11, 2025 - @Test(expected = IndexOutOfBoundsException.class) public void whenSecondIndexEqualToLengthOfString_thenCorrect() { String s = "Welcome to Baeldung"; String sub = s.substring(0, 20); } Next »Java String.toLowerCase() « PreviousJava String.subSequence() Baeldung Pro – NPI EA (cat = Baeldung) Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience: >> Explore a clean Baeldung ·
🌐
Simplilearn
simplilearn.com › home › resources › software development › all you should know about substring in java
All You Should Know About Substring in Java | Simplilearn
September 11, 2025 - 3. Deleting the first character : Strings in Java start at index 0, i.e., the first character is 0 indexed. If you need to remove the first character, use substring(1).
Address: 5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
🌐
CodeGym
codegym.cc › java blog › strings in java › substring in java
Java String substring()
July 23, 2024 - Java Substring in general is a contiguous sequence of characters inside the String. It could be a part of the String or the whole String as well. What is substring in Java? This language has a substring() method, or rather two methods, thanks to Java method overloading.