If your string is Welcome to Java, str.substring(5) will give you the string from index 5 up to the last ch so your output will be me to Java. Index starts from 0. Position and index are used in the same context(both start from 0: zeroth position or zeroth index means the same) Also, str.substring(start index, end index) will return you the substring from start index to (end index -1) character. In our case str.substring(5,14) will return me to Jav

Answer from Saurabh S. Rewaskar on Stack Overflow
🌐
W3Schools
w3schools.com › java › ref_string_substring.asp
Java String substring() Method
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Practice Problems Java Server Java Syllabus Java Study Plan Java Interview Q&A ... The substring() method returns a substring from the string.
🌐
GeeksforGeeks
geeksforgeeks.org › java › substring-in-java
Java String substring() Method - GeeksforGeeks
April 11, 2025 - Explanation: In the above code example, we use the substring(int beginIndex) method and specify the start index which is 8 and it didn't specified the end index. So it will make substring from end of the given string and we get the substring as "Geeks". ... Example 2: Java program to extract a substring from specified start index to specified end index using substring(int beginIndex, int endIndex).
Top answer
1 of 2
2

If your string is Welcome to Java, str.substring(5) will give you the string from index 5 up to the last ch so your output will be me to Java. Index starts from 0. Position and index are used in the same context(both start from 0: zeroth position or zeroth index means the same) Also, str.substring(start index, end index) will return you the substring from start index to (end index -1) character. In our case str.substring(5,14) will return me to Jav

2 of 2
2

In java, the index start from 0. For the example you gave, let's say you created a string like this.

String s = "Welcome to Java";

So, this string has 15 characters in it. If you call below, it will return 15.

s.length()

If you draw a table, it should looks like this:

    -------------------------------------------
    Index     | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
    ------------------------------------------
    character | W | e | l | c | o | m | e |   |
    -------------------------------------------

    ---------------------------------------
    Index     | 8 | 9 | 10| 11| 12| 13| 14|
    ---------------------------------------
    character | t | o |   | J | a | v | a |
    ---------------------------------------

At position 5, since the java start the index with 0, you now know that you have m at position (or index ) at 5, which is 6th character.

    ---------------
    Index     | 5 |
    ---------------
    character | m |
    ---------------

Usually, in java, position and index means same. However, if the person (whoever asked this question to trick you) say "we use the term 'position 5,' (so he meant 5th character in the string as position 5), then you should start the substring at index of 4.

Yes. it is confused. However, when we say position 5, we usually mean the 6th character where index started at 0.

---------------
Index     | 4 |
---------------
character | o |
---------------

Now, let's assume that the person (who asked this question) meant the position 5 as 5th character in the string. Then, you will need to think that, your start index to substring is 4.

So, if the person would like to substring from the nth character in a string (where n is a number between 1 to string length), then you want to start substring at n-1, because, as I said, the index starts at 0, in Java.

Now, let's look at the substring. Here is the documentation about the substring method: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html

Since you would like print a substring of given string, from index 4, you will call...

System.out.println(s.substring(4));

What is the output? "me to Java." ? I this case, the output should be

"ome to Java"

However, if you meant the position 5, same as index 5, then you will call...

System.out.println(s.substring(5));

Which will print

"me to Java"
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › String.html
String (Java Platform SE 8 )
April 21, 2026 - The substring begins with the character at the specified index and extends to the end of this string. ... "unhappy".substring(2) returns "happy" "Harbison".substring(3) returns "bison" "emptiness".substring(9) returns "" (an empty string)
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-string-substring
Master Java Substring Method: Examples, Syntax, and Use Cases | DigitalOcean
February 20, 2025 - Java String substring method is overloaded and has two variants. substring(int beginIndex): This method returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string.
🌐
CodingBat
codingbat.com › doc › java-string-substring.html
Java Substring v2
There is a more complex version of substring() that takes both start and end index numbers: substring(int start, int end) returns a string of the chars beginning at the start index number and running up to but not including the end index.
Find elsewhere
🌐
Codecademy
codecademy.com › docs › java › strings › .substring()
Java | Strings | .substring() | Codecademy
April 28, 2025 - The .substring() method in Java returns a portion of a string from a specified starting index to an (optional) ending index.
🌐
The Renegade Coder
therenegadecoder.com › code › be-careful-with-strings-substring-method-in-java
Be Careful with String’s Substring Method in Java – The Renegade Coder
May 26, 2020 - Returns a new string that is a substring of this string. 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.
🌐
CodeGym
codegym.cc › java blog › strings in java › java string indexof()
Java String indexOf()
December 26, 2024 - Found 'a' at position: 1 Found ... programming language. Java is widely used in web development" and then used the indexOf() method to find all occurrences of the character 'a' or the substring "Java" in the str...
🌐
CodeGym
codegym.cc › java blog › strings in java › substring in java
Java String substring()
As you probably know, String is an immutable Class and to get the substring Java used this immutability earlier in JDK 6. The Object of type String inside is just an array of characters, or rather, contains an array of characters. At the time of JDK 6, two more variables were stored there: the number of the first character in the character array and their quantity. Thus, in JDK 6, String had three fields of char value [] (character array), int offset (index of the first character in the array), and int count (the number of characters in the array).
Published   July 23, 2024
🌐
DevGenius
blog.devgenius.io › java-substring-a-comprehensive-guide-082192995d1e
Java Substring Failed When Input Changed Shape | Dev Genius
1 week ago - A Java substring story showing how indexes, Unicode, validation gaps, nulls, and changing input formats create production parsing bugs.
🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › String › indexOf
Java String indexOf() - Find Character Index | Vultr Docs
December 17, 2024 - The indexOf() method in Java is a powerful tool used to find the position of a specific character or substring within a string. This function returns the index within the calling String object of the first occurrence of the specified value, ...
🌐
Oracle
docs.oracle.com › javase › tutorial › java › data › manipstrings.html
Manipulating Characters in a String (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)
Indices begin at 0, so the character at index 9 is 'O', as illustrated in the following figure: If you want to get more than one consecutive character from a string, you can use the substring method.
🌐
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.
🌐
Reddit
reddit.com › r/learnprogramming › [java] best practice to obtain substring using index of a character that may not be in original string.
r/learnprogramming on Reddit: [JAVA] Best practice to obtain substring using index of a character that may not be in original string.
April 10, 2015 -

I am provided a string. This string is a comma separated list of IDs. The list could have one ID, or it could have 1000 IDs (or any number of IDs). I need to determine how many characters the first ID is, whether it's alone or the first of many.

Currently I have a substring that uses indexOf as the end index, and uses the index of a comma. If there is no comma then the damn thing blows up. I have an OR clause that basically says "If this string up to the first comma is 5 digits OR the entire string is 5 digits, then do something".

I thought this would take care of the scenario with no comma. It doesn't. What is the best way to handle this?

EDIT I would share the code, but this is a massive application for work, and I'm guessing I'd get fired for sharing it, and according to the rules of this sub can't share code that isn't runnable. So while I REALLY think the one line I'm having issue with will help illustrate my problem very well, according to the sub rules I can't share it.

🌐
Programiz
programiz.com › java-programming › library › string › indexof
Java String indexOf()
To find the index of the specified ... - if fromIndex is passed, the str string is searched starting from this index · returns the index of the first occurrence of the specified character/string · returns -1 if the specified character/string is not found....
🌐
iO Flood
ioflood.com › blog › indexof-java
Java's String IndexOf Method: A Detailed Usage Guide
February 26, 2024 - The indexOf() method in Java is a part of the String class and is used to find the position of a character or substring in a string. The method returns the index within this string of the first occurrence of the specified character or substring.
🌐
freeCodeCamp
freecodecamp.org › news › indexof-in-java-how-to-find-the-index-of-a-string-in-java
indexOf in Java – How to Find the Index of a String in Java
March 24, 2022 - In the next example, we'll understand how the public int indexOf(String str) method which returns the index of a substring works.
🌐
How to do in Java
howtodoinjava.com › home › string › java string substring()
Java String substring() with Examples - HowToDoInJava
January 10, 2023 - If we pass the same index for both arguments, an empty string is returned. In the following program, we are finding the substring between the indices 0 and 5. Remember that end index is exclusive, so the character at end index is not included in the substring.