Java Substring Example: Master String Slicing with Code Snippets

Robert Jul 13, 2026

Mastering strings in Java often involves working with substrings, which are portions of a larger string. The String class in Java provides a number of methods to extract substrings. Let's dive into Java substring examples to better understand how to manipulate strings.

5 Examples of substring() in Java
5 Examples of substring() in Java

The most common way to extract a substring in Java is by using the substring method. It requires the start and end indices of the substring you want to extract.

Java substring example | Method, indexof, Find from end - EyeHunts
Java substring example | Method, indexof, Find from end - EyeHunts

Extracting Substrings Using Indexes

The substring method with two parameters begins extracting the substring from the start index up to, but not including, the end index.

Java
Java

Here's a simple example:

Extracting a Substring Up to a Specific Length

java for complete beginners - substring
java for complete beginners - substring

Let's say you have a string String text = "Hello, World!" and you want to extract the first five characters. You would use text.substring(0, 5), which returns "Hello".

The start index 0 indicates the very first character of the string. The end index 5 indicates the index after the last character you want. So, this substring method will return everything from the first character up to, but not including, the sixth character.

Extracting a Substring Starting From an Index

String Methods in Java Explained | Java Programming Basics
String Methods in Java Explained | Java Programming Basics

If you want to extract a substring starting from a specific index all the way to the end of the string, you can use the substring method with only one parameter.

For example, to extract everything from the sixth character onward, you would use text.substring(5). This returns "World!" starting from the sixth character.

Other Useful Substring Methods

Java Programming CheatsheetIntro to Java Programming: An Interdisciplinary Approach and Computer Science: An Interdisciplinary Approach by Sedgewick and Wayne
Java Programming CheatsheetIntro to Java Programming: An Interdisciplinary Approach and Computer Science: An Interdisciplinary Approach by Sedgewick and Wayne

Java offers other convenient methods for handling substrings. Let's explore a couple of them.

Using lastIndexOf to Determine Substring Limits

Strings in Java | Difference Between String, StringBuffer & StringBuilder Explained
Strings in Java | Difference Between String, StringBuffer & StringBuilder Explained
Java Strings - Learn the Essential Methods with its Syntax! - TechVidvan
Java Strings - Learn the Essential Methods with its Syntax! - TechVidvan
Java String Methods
Java String Methods
Java Strings Cheat Sheet ☕ | Master String Problems for Coding Interviews
Java Strings Cheat Sheet ☕ | Master String Problems for Coding Interviews
java for complete beginners - more on java strings
java for complete beginners - more on java strings
How to replace characters and substring in Java? String.replace(), replaceAll() and replaceFirst() Example
How to replace characters and substring in Java? String.replace(), replaceAll() and replaceFirst() Example
How to replace a substring in Java? String replace() method Example Tutorial
How to replace a substring in Java? String replace() method Example Tutorial
Longest Common Substring
Longest Common Substring
☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
Java String  Contains
Java String Contains
[Solved] How to count Vowels and Consonants in Java String Word? Example
[Solved] How to count Vowels and Consonants in Java String Word? Example
Java Cheat Sheet: Operators, Scanner & User Input ☕
Java Cheat Sheet: Operators, Scanner & User Input ☕
Java Type Promotion, Type Casting & Conversion Notes
Java Type Promotion, Type Casting & Conversion Notes
Java Bootcamp London & UK – Part Time Java Developer Program
Java Bootcamp London & UK – Part Time Java Developer Program
Longest Substring Without Repeating Characters
Longest Substring Without Repeating Characters
Perfect Java note for new learners
Perfect Java note for new learners
Java Data Types Made Easy for Beginners
Java Data Types Made Easy for Beginners
String concatenation using StringBuilder class
String concatenation using StringBuilder class
String vs StringBuffer vs StringBuilder | Java Comparison Table
String vs StringBuffer vs StringBuilder | Java Comparison Table

The lastIndexOf method helps when you're not sure of the index you want. It searches backward through the string for the specified character or substring and returns its index.

For instance, finding the comma in the text variable above can be done with text.lastIndexOf(','). This method returns the index 2, since Java uses zero-based indexing.

You can use this with substring to extract a substring starting from a specific character, like this: text.substring(text.lastIndexOf(',') + 1). This returns " World!" starting from the comma.

Using replace to Modify Substrings

The replace method lets you replace a substring with another string. This is useful when you want to change a portion of a string. For example, if you want to change "World" in text to "Java", you can use text.replace("World", "Java") which returns "Hello, Java!".

Understanding and practicing these substring methods will help you navigate string manipulation tasks in Java with ease. As you become more comfortable with these techniques, you'll find yourself quickly extracting and modifying substrings according to your needs. Happy coding!