java substring

Java substring

What is a substring?

The java substring() method of String class is used for getting a substring from the string.

public final class String extends Object implements Serializable, Comparable<String>, CharSequence, Constable, ConstantDesc

The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class.

Beginning with JDK 9, all of java.lang is part of the java.base module. The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class.

Java Platform, Standard Edition & Java Development Kit Version 16 API Specification
Module java.base
Package java.lang
Class String

Because String objects are immutable, whenever you want to modify a String, you must either copy it into a StringBuffer or StringBuilder, or use a String method that constructs a new copy of the string with your modifications complete.

Syntax 1

public String substring(int startIndex)

This method returns new String object containing the substring of the given string from specified startIndex (inclusive).

java 16 docs

Syntax 2

public String substring(int startIndex, int endIndex)

This method returns new String object containing the substring of the given string from specified startIndex to endIndex.

java 16 docs
https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/lang/String.html#substring(int)