Curriculum
Course: Java Basic
Login

Curriculum

Java Basic

Java Home

0/1

Java Introduction

0/1

Java Get Started

0/1

Java Syntax

0/1

Java Comments

0/1

Java Type Casting

0/1

Java Operators

0/1

Java Booleans

0/1

Java Switch

0/1

Java Break / Continue

0/1

Java Errors and Exception

0/1
Text lesson

substring()

Example

Provide a section of a string as a substring.

String myStr = "Hello, World!";
System.out.println(myStr.substring(7, 12));

Definition and Usage

The substring() method retrieves a portion of the string.

If the end parameter is omitted, the substring extends to the end of the string.

Syntax

One of the following:

public String substring(int start, int end)
public String substring(int start)

Parameter Values

Parameter

Description

start

Necessary: The index indicating the character where the substring begins.

end

Necessary: The index indicating the character where the substring begins.

Technical Details

Returns

A string that contains a portion of the original string as a substring.

Throws

The IndexOutOfBoundsException occurs if either the start or end values are negative, exceed the length of the string, or if start is greater than end.

Java Version

Any.