I just announced the new Spring 5 modules in REST With Spring:

>> CHECK OUT THE COURSE

This article is part of a series:
• Java String.String()
• Java String.subSequence()
• Java String.codePointCount()
• Java String.codePointAt()
• Java String.concat()
• Java String.contains()
• Java String.copyValueOf()
• Java String.endsWith()
• Java String.format()
• Java String.getBytes()
• Java String.indexOf()
• Java String.intern()
• Java String.isEmpty()
• Java String.lastIndexOf()
• Java String.regionMatches()
• Java String.replace()
• Java String.replaceAll()
• Java String.split()
• Java String.startsWith()
• Java String.subSequence()
• Java String.substring()
• Java String.toLowerCase()
• Java String.toUpperCase()
• Java String.trim()
• Java String.valueOf()

The method subSequence() obtains a part of a String given the starting index and the length of the result. The method SubSequence() behaves in the same way as substring().

The only difference is that it returns a CharSequence instead of a String.

Available Signatures

public CharSequence subSequence(int beginIndex, int endIndex)

Example

@Test
public void whenCallSubSequence_thenCorrect() {
    String s = "Welcome to Baeldung";
    
    assertEquals("Welcome", s.subSequence(0, 7));
}

I just announced the new Spring 5 modules in REST With Spring:

>> CHECK OUT THE LESSONS