CKTIS / Java
String.substring — extracting part of a string
String.substring returns part of a string without changing the original. The start is inclusive and the end exclusive; indices count UTF-16 code units.
Version: Java SE 21; repository runtime, production runtime not attested
Syntax
String substring(int beginIndex)
String substring(int beginIndex, int endIndex)String.substring(beginIndex, endIndex) returns the range [beginIndex, endIndex), with length endIndex - beginIndex. Indices start at zero. String.substring(beginIndex) returns the suffix through the end. The original remains unchanged.
Require 0 <= beginIndex <= endIndex <= length(). The one-argument overload requires 0 <= beginIndex <= length(). Invalid bounds cause IndexOutOfBoundsException. Equal bounds, including at the end, produce an empty string.
Indices count UTF-16 code units (char), not necessarily complete Unicode characters. A boundary can split a surrogate pair; do not treat these indices as user-perceived character positions.
Examples
public class Main {
public static void main(String[] args) {
String text = "abcdef";
System.out.println(text.substring(1, 4));
System.out.println(text.substring(4));
System.out.println(text.substring(6).isEmpty());
System.out.println(text);
}
}Result
bcd ef true abcdef
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.