Java .subSequence()
Published Feb 17, 2023
.subSquence() is a String method that returns a new character sequence that is a substring of a given string.
Syntax
str.subSequence(int startIndex, int endIndex)
The .subSequence() method will return a substring of the str string from startIndex to endIndex - 1.
The .subSquence() method takes two parameters:
startIndex- the starting index (inclusive).endIndex- the ending index (exclusive).
Both parameters must be integers.
Example
The following example will return a substring of the str string.
class Main {public static void main(String[] args) {String str = "Java Programming";System.out.println(str.subSequence(0, 4));}}
This will print:
Java
Learn Java on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
- Beginner Friendly.17 hours