Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit c996ea5

Browse files
authored
Add second blogpost for the 2019-12-12
I have added the blogpost 'substring() Method in Java (With example)' on 2019-12-12. This is the second blogpost on the day.
1 parent 8933a89 commit c996ea5

File tree

1 file changed

+185
-0
lines changed

1 file changed

+185
-0
lines changed
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
2+
---
3+
layout: post
4+
title: substring() Method in Java (With example)
5+
author: gaurav
6+
categories: [ Java, Core Java, String]
7+
description: The String class in Java is one of the most important classes in Java. After reading this article you will be able to use the 'substring()` method of the String class.
8+
9+
---
10+
The String class in Java is one of the most important classes in Java. After reading this article you will be able to use the 'substring()` method of the String class.
11+
12+
I have given two examples with the pictorial explanation to make this concept easy to understand.
13+
14+
## Introduction
15+
16+
let us understand what does mean by 'substring'. As per the [Wikipedia](https://en.wikipedia.org/wiki/Substring),
17+
18+
“A **substring** is a contiguous sequence of characters within a string. For instance, “the best of” is a **substring** of “It was the best of times”.
19+
20+
Java `substring()` method returns a 'substring' of the specified string. The creation of the 'substring' depends on the parameter passed to the `substring()` method.
21+
22+
In Java, the `substring()` method is the overloaded method and it has two variants.
23+
24+
1. `substring(int beginIndex)`
25+
2. `substring(int beginIndex, int endIndex)`
26+
27+
We will learn about both variants one by one.
28+
29+
## 1. `substring(int beginIndex)`
30+
31+
The first variant of the `substring()` method accepts only one parameter. i.e. `beginIndex` The substring will start from the specified `beginIndex` and it will extend to the end of the string.
32+
33+
```
34+
public String substring(int beginIndex)
35+
36+
Returns a string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string.
37+
38+
Parameters:
39+
beginIndex - the beginning index, inclusive.
40+
41+
Returns:
42+
the specified substring.
43+
44+
Throws:
45+
IndexOutOfBoundsException - if beginIndex is negative or larger than the length of this String object.
46+
```
47+
48+
Please note that the `beginIndex` parameter is **inclusive**, which means the character at the `beginIndex` will be the part of the substring.
49+
50+
So, it will return a substring which is started from the character at the `beginIndex` and it will extend to the end of the string.
51+
52+
I will take a simple example to explain to same.
53+
54+
If the given string is “codeRolls.com” and we applied the substring() method with the `beginIndex` as `4`, it will return the substring “Rolls.com”.
55+
56+
```
57+
"codeRolls".substring(4) returns "Rolls.com"
58+
```
59+
60+
See the pictorial explanation shown below.
61+
62+
![substring() method explanation with both parameter i.e. beginIndex and endIndex](/assets/images/2019-12-12-java-substring-method/substring-method-with-beginIndex.png)
63+
64+
substring() method explanation with one parameter i.e. beginIndex
65+
66+
Let us see the full java code for the example stated above.
67+
68+
```java
69+
public class SubstringExample {
70+
71+
public static void main(String[] args) {
72+
73+
String name = "codeRolls.com";
74+
75+
String result = name.substring(4);
76+
77+
System.out.println(result);
78+
}
79+
80+
}
81+
```
82+
83+
Output:
84+
85+
```
86+
Rolls.com
87+
```
88+
89+
### Note:
90+
91+
`substring(int beginIndex)` will throw an `IndexOutOfBoundsException` if the `beginIndex` is negative or larger than the length of the string object.
92+
93+
i.e. It will throw an exception if
94+
95+
`beginIndex` is negative OR `beginIndex` > `string.length()`
96+
97+
## 2. `substring(int beginIndex, int endIndex)`
98+
99+
This variant of the substring method will accept two-parameter, `beginIndex` and `endIndex`. This method will return the substring which starts from the `beginIndex` and will end at `endIndex - 1`.
100+
101+
Yes, the substring will extend till the `endIndex-1` because the parameter `endIndex` is **exclusive** in this variant of the method. This means the character at the `endIndex` will not be part of the substring.
102+
103+
```
104+
public String substring(int beginIndex, int endIndex)
105+
106+
Returns a string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.
107+
108+
Parameters:
109+
beginIndex - the beginning index, inclusive.
110+
endIndex - the ending index, exclusive.
111+
112+
Returns:
113+
the specified substring.
114+
115+
Throws:
116+
IndexOutOfBoundsException - if the beginIndex is negative, or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex.
117+
```
118+
119+
You can calculate the length of the substring using `beginIndex` and `endIndex`. It will be `endIndex``beginIndex`.
120+
121+
I will take a simple example to explain it to you.
122+
123+
Let us take a simple string like “codeRolls.com” and I will apply the `substring()` method with the parameter `beginIndex` as `4` and `endIndex` as `9` it will return the substring “Rolls”.
124+
125+
```
126+
"codeRolls".substring(4, 9) returns "Rolls"
127+
```
128+
129+
See the pictorial explanation shown below.
130+
131+
![substring() method explanation with both parameter i.e. beginIndex and endIndex](/assets/images/2019-12-12-java-substring-method/substring-method-with-beginIndex-and-endIndex.png)
132+
133+
Let us see the full java code for the example stated above.
134+
135+
```java
136+
public class SubstringExampleWithBothIndex {
137+
138+
public static void main(String[] args) {
139+
140+
String name = "codeRolls.com";
141+
142+
String result = name.substring(4, 9);
143+
144+
System.out.println(result);
145+
}
146+
147+
}
148+
```
149+
150+
Output:
151+
152+
```
153+
Rolls
154+
```
155+
156+
### Note:
157+
158+
`substring(int beginIndex, int endIndex)` method will throw `IndexOutOfBoundsException` exception if `beginIndex` is negative, or `beginIndex` larger than the `endIndex` or `endIndex` is larger than the length of the string object.
159+
160+
i.e `substring(int beginIndex, int endIndex)` will throw an exception if
161+
162+
`beginIndex` is negative OR `beginIndex` > `endIndex` OR `endIndex` > `string.length()`
163+
164+
## Conclusion
165+
166+
In Java `substring()` method has two variants.
167+
168+
1. `substring(int beginIndex)`
169+
2. `substring(int beginIndex, int endIndex)`
170+
171+
`substring(int beginIndex)` returns the substring of this string which will start from the character at the `beginIndex` and will extend to the end of the string. Here, the parameter `beginIndex` is inclusive.
172+
173+
`substring(int beginIndex, int endIndex)` returns the substring of this string which will start from the character at the `beginIndex` and will extend till the character at the `endIndex - 1`. Here, the parameter `beginIndex` is inclusive while the parameter `endIndex` is exclusive.
174+
175+
If you found this article worth, please [Give me a cup of Coffee ☕](https://paypal.me/GauravKukade)
176+
177+
If you have any queries about the `substring()` method please write it below in the comment section.
178+
179+
#### Related Articles
180+
181+
- [Learn About Java String Pool And intern() Method](https://coderolls.com/java-string-pool-and-intern-method/)
182+
- [How Do I Compare Strings In Java](https://coderolls.com/compare-strings-in-java/)
183+
- [How To Reverse A String In Java (5 ways)](https://coderolls.com/reverse-a-string-in-java/)
184+
- [Compare Two Strings Lexicographically In Java](https://coderolls.com/compare-two-strings-lexicographically-in-java/)
185+
- [Introduction to Java Technology (Language and Platform)](https://coderolls.com/java-introduction/)

0 commit comments

Comments
 (0)