You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/java/concepts/strings/terms/substring/substring.md
+73-19
Original file line number
Diff line number
Diff line change
@@ -2,17 +2,20 @@
2
2
Title: '.substring()'
3
3
Description: 'Returns a part of the string specified through a starting index and an optional ending index.'
4
4
Subjects:
5
+
- 'Code Foundations'
5
6
- 'Computer Science'
6
7
Tags:
7
8
- 'Characters'
8
-
- 'Strings'
9
9
- 'Methods'
10
+
- 'Strings'
10
11
CatalogContent:
11
12
- 'learn-java'
12
13
- 'paths/computer-science'
13
14
---
14
15
15
-
The **`.substring()`** method returns a part of the string from some starting index to an (optional) ending index. If an ending index is not provided, the substring will be from the starting index to the end of the original string.
16
+
The **`.substring()`** method in Java returns a portion of a string from a specified starting index to an (optional) ending index. If an ending index is not provided, the substring extends from the starting index to the end of the original string.
17
+
18
+
This method is commonly used for text processing, data parsing, and string manipulation tasks. It offers two variants that allow the extraction of characters either from a specific starting index or between a range of indices.
16
19
17
20
## Syntax
18
21
@@ -21,38 +24,89 @@ string.substring(startIndex);
21
24
string.substring(startIndex, endIndex);
22
25
```
23
26
24
-
`.substring()` returns characters from `startIndex` up to, but not including, the character at `endIndex`.
25
-
26
-
If `endIndex` is omitted, `.substring()` returns characters from `startIndex` to the end of the string.
27
-
28
-
If `startIndex` and `endIndex` are equal, `.substring()` returns an empty string.
27
+
-`.substring()` returns characters from `startIndex` up to, but does not include, the character at `endIndex`.
28
+
- If `endIndex` is omitted, `.substring()` returns characters from `startIndex` to the end of the string.
29
+
- If `startIndex` and `endIndex` are equal, `.substring()` returns an empty string.
29
30
30
31
A [`StringIndexOutOfBoundsException`](https://www.codecademy.com/resources/docs/java/errors/stringindexoutofboundsexception) is thrown if any of the following are true:
31
32
32
33
-`startIndex` is greater than `endIndex`
33
34
-`startIndex` is greater than the length of the original string
34
35
-`startIndex` or `endIndex` is negative
35
36
36
-
## Example
37
+
## Example 1: Extracting Characters From a Specific Index to the End
37
38
38
-
The following example uses `.substring()`to display a substring of a given string.
39
+
This example demonstrates how to extract a substring using `substring(beginIndex)`method, starting from a specific index to the end of the string:
System.out.println("Substring starting from index 7: "+ result);
53
+
49
54
}
50
55
}
51
56
```
52
57
53
-
This produces the following output:
58
+
This example results in the following output:
54
59
55
60
```shell
56
-
cool!
57
-
Java
61
+
Original string: Hello, Java!
62
+
Substring starting from index 7: Java!
58
63
```
64
+
65
+
In this example, `substring(7)` extracts all characters from index 7 (inclusive) to the end of the string.
66
+
67
+
## Example 2: Extracting Characters Between Two Indices
68
+
69
+
This example demonstrates how to extract a substring using `substring(beginIndex, endIndex)` method between a specific range of indices:
70
+
71
+
```java
72
+
classMain {
73
+
74
+
publicstaticvoidmain(String[] args) {
75
+
76
+
String text ="Programming with Java";
77
+
78
+
String result = text.substring(0, 11);
79
+
80
+
System.out.println("Original string: "+ text);
81
+
82
+
System.out.println("Substring from index 0 to 11: "+ result);
83
+
84
+
}
85
+
}
86
+
```
87
+
88
+
This example results in the following output:
89
+
90
+
```shell
91
+
Original string: Programming with Java
92
+
Substring from index 0 to 11: Programming
93
+
```
94
+
95
+
In this example, `substring(0, 11)` extracts characters from index 0 (inclusive) to index 11 (exclusive).
96
+
97
+
## Frequently Asked Questions
98
+
99
+
### 1. Does a substring start at 0 in Java?
100
+
101
+
Yes, string indexing in Java starts at 0, so the first character of a string is at index 0. When using the `substring()` method, the `beginIndex` parameter follows this zero-based indexing.
102
+
103
+
### 2. Is a substring in Java inclusive?
104
+
105
+
The `beginIndex` parameter in the `substring()` method is inclusive, meaning the character at that index will be included in the result. However, the `endIndex` parameter is exclusive, meaning the character at that index will not be included in the result.
106
+
107
+
### 3. Is an empty string a substring?
108
+
109
+
Yes, an empty string is a valid substring in Java. You can get an empty string by:
110
+
111
+
- Using the same value for both `beginIndex` and `endIndex` in `substring(beginIndex, endIndex)`
112
+
- Using `substring(str.length())`, which returns an empty string since it starts at the end of the string
0 commit comments