The String.substring() methods do not examine the content of the substring, so what you are describing is likely a bug in your code. You can post the minimal amount of code necessary to reproduce the problem if you'd like some help troubleshooting.


Given the code and information given in the update and comments, this is what I'd expect:

String actnum = "03KL352"; /* Maybe actnum is actually entered via a JSP. */
System.out.println(actnum); /* Prints 03KL352 */
String ccode = actnum.substring(1,2); /* Assign characters [1,2) to ccode. */
System.out.println(ccode); /* Prints 3 */

Remember, Java's string indexes are zero-based. The first character is at index 0, the next at index 1. Also, the substring method takes two character indexes; the first is included in the new substring, the second is not—it is the index of the character after the last character in the new substring. So, the length of the new substring is end - start.

Answer from erickson on Stack Overflow
Top answer
1 of 3
14

The String.substring() methods do not examine the content of the substring, so what you are describing is likely a bug in your code. You can post the minimal amount of code necessary to reproduce the problem if you'd like some help troubleshooting.


Given the code and information given in the update and comments, this is what I'd expect:

String actnum = "03KL352"; /* Maybe actnum is actually entered via a JSP. */
System.out.println(actnum); /* Prints 03KL352 */
String ccode = actnum.substring(1,2); /* Assign characters [1,2) to ccode. */
System.out.println(ccode); /* Prints 3 */

Remember, Java's string indexes are zero-based. The first character is at index 0, the next at index 1. Also, the substring method takes two character indexes; the first is included in the new substring, the second is not—it is the index of the character after the last character in the new substring. So, the length of the new substring is end - start.

2 of 3
1

Based on the code that you have in the question, you probably want the first two characters of the string. Assuming this to be the case, then the code that you want is:

ccode = actnum.substring(0,2);

The Javadoc for substring states that it returns the characters from the index specified by first argument up till, but not including, the index specified by the second argument.

The first two characters of the string would be actnum.substring(0,2). The first argument is 0-based. The second argument is also 0-based, but is not included in the result.

🌐
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 “Saket Saurav” and try to fetch 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) and so on.
Discussions

java - Substring Method - Stack Overflow
It works but I thought that the substring method starts at 0 and goes from there so that substring(0,0) would include just the first letter and likewise substring(0,4) would include 0 1 2 3 4 letters for the first 5. Or does it not include the final number in the output? Copyimport java.util.... More on stackoverflow.com
🌐 stackoverflow.com
java - Why is "out of range" not thrown for 'substring(startIndex, endIndex)' - Stack Overflow
We know that Gosling based the Java syntax on the C/C++ languages for familiarity. From C+++ string class http://www.cplusplus.com/reference/string/string/substr/ we see the method definition is: string substr (size_t pos = 0, size_t len = npos) const; More on stackoverflow.com
🌐 stackoverflow.com
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
Removing character of a string using the substring method at a random index?
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
6
0
April 1, 2022
🌐
CodingBat
codingbat.com › doc › java-string-substring.html
Java Substring v2
String str = "Hello"; String a = str.substring(2, 4); // a is "ll" (not "llo") String b = str.substring(0, 3); // b is "Hel" String c = str.substring(4, 5); // c is "o" -- the last char
🌐
GeeksforGeeks
geeksforgeeks.org › java › substring-in-java
Java String substring() Method - GeeksforGeeks
April 11, 2025 - Explanation: In the above example, we use the substring(beginIndex, endIndex) the substring starts at index 0 and ends at index 7, but the character at index 7 is excluded.
🌐
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.
🌐
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.
Find elsewhere
🌐
Java67
java67.com › 2016 › 01 › 5-examples-of-substring-in-java.html
5 Examples of substring() in Java | Java67
public String substring(int beginIndex) ... and goes till endIndex -1. Since String in Java is zero indexes based, beginIndex can be from 0 to length of String....
🌐
BeginnersBook
beginnersbook.com › 2013 › 12 › java-string-substring-method-example
Java String substring() Method with examples
For example: "Chaitanya".substring(2) would return "aitanya". The beginIndex is inclusive, that is why the character present at the index 2 is included in the substring. This method throws IndexOutOfBoundsException If the beginIndex is less than zero or greater than the length of String ...
🌐
Tutorialspoint
tutorialspoint.com › java › lang › string_substring.htm
Java - String substring() Method
package com.tutorialspoint; public class StringDemo { public static void main(String[] args) { String str = "This is tutorials point"; String substr = ""; // prints the substring after index 7 substr = str.substring(7); System.out.println("substring = " + substr); // prints the substring after index 0 i.e whole string gets printed substr = str.substring(0); System.out.println("substring = " + substr); } } If you compile and run the above program, it will produce the following result − · substring = tutorials point substring = This is tutorials point · Following is an example to show the use of substring() method in Java by finding the substring of the provided string.
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-string-substring
Master Java Substring Method: Examples, Syntax, and Use Cases | DigitalOcean
Learn how to use the Java substring method. Explore syntax, practical examples, and common errors to handle substrings effectively.
🌐
Great Learning
mygreatlearning.com › blog › it/software development › java substring: an in-depth guide
Java Substring: An In-depth Guide
September 3, 2024 - If an end index is provided, the ... end of the string. Remember, Java employs zero-based indexing, meaning the first character of a string is at index 0, not 1....
🌐
Coderanch
coderanch.com › t › 239937 › certification › substring
substring question (OCPJP forum at Coderanch)
November 13, 2002 - programming forums Java Mobile ... our volunteer staff, including ... ... System.out.println( "Don".substring(0,0) ); here substring should try to get a string from index 0 to index -1....
🌐
HappyCoders
happycoders.eu › home › java › java substring() method
Java substring() Method
June 12, 2025 - Experienced Java developers familiar ... 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)...
🌐
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.
🌐
Medium
maheshjtp.medium.com › what-is-a-substring-in-java-syntax-applications-of-java-substring-9abbf79350e9
What is a Substring in Java? Syntax/ Applications of Java Substring | by Mahesh Sharma | Medium
March 7, 2025 - In this example, we have a string original String containing the text “Java is amazing!”. We then use the substring() method to extract two substrings: substring1, which starts from index 5 (“i”) to the end of the string, resulting in “is amazing!”. substring2, starting from index 0 (“J”) up to (but not including) index 4 (“a”), resulting in “Java”.
🌐
Javatpoint
javatpoint.com › java-string-substring
Java String substring()
Java String substring() method with method signature and examples of concat, compare, touppercase, tolowercase, trim, length, equals, split, string substring in java etc.
🌐
DevGenius
blog.devgenius.io › java-substring-a-comprehensive-guide-082192995d1e
Java Substring Failed When Input Changed Shape | Dev Genius
July 6, 2026 - A Java substring story showing how indexes, Unicode, validation gaps, nulls, and changing input formats create production parsing bugs.
🌐
IronPDF
ironpdf.com › ironpdf for java › ironpdf for java blog › java help › java substring method
Java Substring Method (How It Works For Developers)
April 22, 2026 - public String substring(int beginIndex): This method creates a new string starting from the beginIndex to the end of the original string. It's important to note that the string index in Java starts at 0.