Skip to content

Commit fe93216

Browse files
[Edit] Java Substrings (#6615)
* Update substring.md * Formating fix * minor changes * Update substring.md * formating fix ---------
1 parent 18e973c commit fe93216

File tree

1 file changed

+73
-19
lines changed

1 file changed

+73
-19
lines changed

content/java/concepts/strings/terms/substring/substring.md

+73-19
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@
22
Title: '.substring()'
33
Description: 'Returns a part of the string specified through a starting index and an optional ending index.'
44
Subjects:
5+
- 'Code Foundations'
56
- 'Computer Science'
67
Tags:
78
- 'Characters'
8-
- 'Strings'
99
- 'Methods'
10+
- 'Strings'
1011
CatalogContent:
1112
- 'learn-java'
1213
- 'paths/computer-science'
1314
---
1415

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.
1619

1720
## Syntax
1821

@@ -21,38 +24,89 @@ string.substring(startIndex);
2124
string.substring(startIndex, endIndex);
2225
```
2326

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.
2930

3031
A [`StringIndexOutOfBoundsException`](https://www.codecademy.com/resources/docs/java/errors/stringindexoutofboundsexception) is thrown if any of the following are true:
3132

3233
- `startIndex` is greater than `endIndex`
3334
- `startIndex` is greater than the length of the original string
3435
- `startIndex` or `endIndex` is negative
3536

36-
## Example
37+
## Example 1: Extracting Characters From a Specific Index to the End
3738

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:
3940

4041
```java
41-
// Example.java
42-
public class Example {
42+
class Main {
43+
4344
public static void main(String[] args) {
44-
String blurb = "Java is cool!";
45-
// Printing last 5 characters of blurb
46-
System.out.println(blurb.substring(blurb.length() - 5));
47-
// Printing first 4 characters of blurb
48-
System.out.println(blurb.substring(0, 4));
45+
46+
String text = "Hello, Java!";
47+
48+
String result = text.substring(7);
49+
50+
System.out.println("Original string: " + text);
51+
52+
System.out.println("Substring starting from index 7: " + result);
53+
4954
}
5055
}
5156
```
5257

53-
This produces the following output:
58+
This example results in the following output:
5459

5560
```shell
56-
cool!
57-
Java
61+
Original string: Hello, Java!
62+
Substring starting from index 7: Java!
5863
```
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+
class Main {
73+
74+
public static void main(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

Comments
 (0)