I hope you can better understand with this example:

Answer from Adrian Adamczyk on Stack Overflow
🌐
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.
🌐
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 - 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.
🌐
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.
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.

🌐
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....
🌐
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....
Find elsewhere
🌐
BeginnersBook
beginnersbook.com › 2013 › 12 › java-string-substring-method-example
Java String substring() Method with examples
str.substring(1): This excludes first character as the index 0 character (first character) is excluded. 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"); System.out.print("Substring after removing first character: "); System.out.println(str.substring(1)); System.out.print("Substring after removing last character: "); System.out.println(str.substring(0, str.length()-1)); } }
🌐
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 till index 17 substr = str.substring(7, 17); System.out.println("substring = " + substr); // prints the substring after index 0 till index 7 substr = str.substring(0, 7); System.out.println("substring = " + substr); } }
🌐
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. 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
🌐
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....
🌐
Net Informations
net-informations.com › java › string › substring.htm
Java String substring()
In other words, the character at the startIndex position is included in the extracted substring, but the character at the endIndex position is not. This exclusive nature of the endIndex parameter means that if you want to remove the last character from a String, you would use substring(0, string.length() - 1).
🌐
HappyCoders
happycoders.eu › home › java › java substring() method
Java substring() Method
June 12, 2025 - original string: string identity : @4c203ea1 string : HappyCoders.eu value[] identity : @71be98f5 value[] : [72, 97, 112, 112, 121, 67, 111, 100, 101, 114, 115, 46, 101, 117] coder : 0 substring: string identity : @96532d6 string : Code value[] identity : @3796751b value[] : [67, 111, 100, 101] coder : 0 substring appended to empty string: string identity : @3498ed string : Code value[] identity : @1a407d53 value[] : [67, 111, 100, 101] coder : 0 substring wrapped with new string: string identity : @3d8c7aca string : Code value[] identity : @3796751b value[] : [67, 111, 100, 101] coder : 0 · Analogous to the previous Java version, string concatenation creates a new byte array, while the String constructor reuses the existing byte array. This article has shown how to use String.substring(), how the method works internally, and how its functionality has changed over time.
🌐
Home and Learn
homeandlearn.co.uk › java › substring.html
java for complete beginners - substring
We have two numbers in the code above, 0 and 2. This means start grabbing characters at position 0 in the string, and stop grabbing when you have two of them. The two characters are then returned and placed in the variable FirstNameChars. If you always want to go right to the end of the string, ...
🌐
How to do in Java
howtodoinjava.com › home › string › java string substring()
Java String substring() with Examples - HowToDoInJava
January 10, 2023 - Pass both indices (begin and end indices) when we need to find a substring starting from the given index position to the given end index position. Please note that: The beginIndex can be greater than the endIndex. Both indices must be greater than or equal to 0.
🌐
Programiz
programiz.com › java-programming › library › string › substring
Java String substring()
If the endIndex is not passed, the substring begins with the character at the specified index and extends to the end of the string · Working of Java String substring() method · Note: You will get an error if · startIndex/endIndex is negative or greater than string's length · startIndex is greater than endIndex · class Main { public static void main(String[] args) { String str1 = "program"; // 1st character to the last character · System.out.println(str1.substring(0)); // program // 4th character to the last character System.out.println(str1.substring(3)); // gram } } class Main { public static void main(String[] args) { String str1 = "program"; // 1st to the 7th character System.out.println(str1.substring(0, 7)); // program // 1st to the 5th character ·