---
title: Substring in Java - Scaler Topics
description: A Substring in Java is a part of a string, and that part can be the whole string also. In this article by Scaler Topics, we have explained Substring in Java with examples.
author: Rishabh Mahajan
category: Java
---
::: main
A substring in java is a contiguous sequence of characters within a string. A substring can consist of any number of characters, from a single character to the entire string.
There are two methods in Java for a substring of a specified string:
* **String substring(startIndex)**
* **String substring(startIndex, endIndex)**
The first method returns a substring that starts from the specified index and goes to the end of the string, while the second method returns a substring that starts from the specified start index and ends at the specified end index (exclusive).
Take an example of a string name : *“Mr Yuvraj Singh”*
In the given string, if you want to remove Mr from the name and use only Yuvraj Singh, then you have to find out the substring of the whole string whose index ranges from 3 to 14 if you consider the `0 based` indexing of the string.

:::
## String substring() Method
Two different methods of signature use substring(), which helps to get a substring in Java. The first method only takes the starting index of the string and returns a new string that is substring, which begins at that startIndex and ends at the end of the string, which means the range of substring is from `startIndex` till the end of the string.
**Syntax:**
```java
String substring(int startIndex)
```
**startIndex:** It defines the starting point of the substring in the specified string.
**Return Value**
The function returns the specified substring from `startIndex` till the end of the string. The return type is `String`.
**Example:**
```java
public class Main {
public static void main(String[] args) {
//consider this string
//here, indexing starts from 0, and space is considered as a char in the string
String s = "Welcome to Scaler";
// 1st method
System.out.println("The substring is: " + s.substring(11));
}
}
```
**Output:**
```plaintext
The substring is: Scaler
```
**Explanation:**
In the above example, It prints “Scaler” as a substring, starting from index `11` and ending at the string's end.
:::
:::section{.main}
## String substring(begIndex, endIndex)
Another variant of the Substring in Java takes parameters that are starting index and ending index and It returns a new string that is a substring of this string, which begins at that `startIndex` and ends at the **endIndex – 1** if the second parameter is given in the function.
:::
:::section{.tip}
**Note:** The `startIndex` is included in the range, and `endIndex` is not included, and indexing of the string is `0` based.
:::
:::section{.main}
**Syntax:**
```java
String substring(int startIndex, int endIndex)
```
- **startIndex:** It defines the starting point of the substring in the specified string.
- **endIndex:** It defines the ending point of the substring in the specified string. The substring ends at `endIndex-1`.
**Return Value:**
The function returns the specified substring from `startIndex` till the end of the string. The return type is `String`.
:::
:::section{.tip}
**Note:** If the start index is negative or the end index is lower than the start index, the method throws `StringIndexOutOfBoundsException`.
:::
:::section{.main}
**Example:**
```java
public class Main {
public static void main(String[] args) {
//consider this string
//here, indexing starts from 0, and space is considered as a char in the string
String s = "Welcome to Scaler";
// 2nd method
System.out.println("The substring is: " + s.substring(0, 7));
}
}
```
**Output:**
```plaintext
The substring is: Welcome
```
**Explanation:**
In the above example, It prints The second part, which will print “`Welcome`” as a substring where it starts from index `0` and ends at index `6`.
:::
:::section{.main}
## String.split() Method
The `split()` method is used to split and extract the substrings based on a delimiter.
**Syntax:**
```java
Public String [] split ( String regex, int limit)
```
**Return Value:**
The `split()` method in java returns an array of strings.
**Example:**
```java
public class Main {
public static void main(String[] args) {
String sentence = "The world is so beautiful";
String[] words = sentence.split(" ", 3);
for (String word: words) {
System.out.println(word);
}
}
}
```
**Output:**
```plaintext
The
world
is so beautiful
```
**Explanation:** This code splits the sentence into words based on spaces but limits the number of splits to 3. So, only the first three words will be extracted and printed.
:::
:::section{.summary}
## Conclusion
* A substring in java is a contiguous sequence of characters within a string.
* Java provides two methods for extracting substrings from a string: String `substring(startIndex)` and String `substring(startIndex, endIndex)`.
* The `substring(startIndex)` method returns a substring that starts from the specified index and goes to the end of the string.
* The `substring(startIndex, endIndex)` method returns a substring that starts from the specified start index and ends at the specified end index (exclusive).
:::