From 976bd4ee1692a5f9bcf13db0ac371a87487d802e Mon Sep 17 00:00:00 2001 From: Radhika-okhade Date: Tue, 22 Apr 2025 23:12:08 +0530 Subject: [PATCH 1/5] Update substring.md --- .../strings/terms/substring/substring.md | 118 +++++++++++++----- 1 file changed, 85 insertions(+), 33 deletions(-) diff --git a/content/java/concepts/strings/terms/substring/substring.md b/content/java/concepts/strings/terms/substring/substring.md index be7617bcd32..5e14e6bc48a 100644 --- a/content/java/concepts/strings/terms/substring/substring.md +++ b/content/java/concepts/strings/terms/substring/substring.md @@ -2,57 +2,109 @@ Title: '.substring()' Description: 'Returns a part of the string specified through a starting index and an optional ending index.' Subjects: + - 'Code Foundations' - 'Computer Science' Tags: - 'Characters' - - 'Strings' - 'Methods' + - 'Strings' CatalogContent: - 'learn-java' - 'paths/computer-science' --- -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. +The **`.substring()`** method in Java 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. + +This method is useful for text processing, data parsing, and string manipulation tasks. There are two variants of this method that allow you to extract characters from a specific starting index or between a range of indices. + +## Syntax + +```pseudo +string.substring(startIndex); +string.substring(startIndex, endIndex); +``` + +- `.substring()` returns characters from `startIndex` up to, but not including, the character at `endIndex`. + +- If `endIndex` is omitted, `.substring()` returns characters from `startIndex` to the end of the string. + +- If `startIndex` and `endIndex` are equal, `.substring()` returns an empty string. + +A [`StringIndexOutOfBoundsException`](https://www.codecademy.com/resources/docs/java/errors/stringindexoutofboundsexception) is thrown if any of the following are true: + +- `startIndex` is greater than `endIndex` +- `startIndex` is greater than the length of the original string +- `startIndex` or `endIndex` is negative + +## Example 1: Extracting Characters From a Specific Index to the End + +This example demonstrates how to extract a substring using `substring(beginIndex)` method starting from a specific index to the end of the string: + +```java +class Main { + + public static void main(String[] args) { + + String text = "Hello, Java!"; + + String result = text.substring(7); + + System.out.println("Original string: " + text); + + System.out.println("Substring starting from index 7: " + result); + + } +} +``` + +This example results in the following output: + +```shell +Original string: Hello, Java! +Substring starting from index 7: Java! +``` + +In this example, `substring(7)` extracts all characters from index 7 (inclusive) to the end of the string. + +## Example 2: Extracting Characters Between Two Indices + +This example demonstrates how to extract a substring using `substring(beginIndex, endIndex)` method between a specific range of indices: + +```java +class Main { + + public static void main(String[] args) { -## Syntax + String text = "Programming with Java"; -```pseudo -string.substring(startIndex); -string.substring(startIndex, endIndex); -``` + String result = text.substring(0, 11); -`.substring()` returns characters from `startIndex` up to, but not including, the character at `endIndex`. + System.out.println("Original string: " + text); -If `endIndex` is omitted, `.substring()` returns characters from `startIndex` to the end of the string. + System.out.println("Substring from index 0 to 11: " + result); -If `startIndex` and `endIndex` are equal, `.substring()` returns an empty string. + } +} +``` -A [`StringIndexOutOfBoundsException`](https://www.codecademy.com/resources/docs/java/errors/stringindexoutofboundsexception) is thrown if any of the following are true: +This example results in the following output: -- `startIndex` is greater than `endIndex` -- `startIndex` is greater than the length of the original string -- `startIndex` or `endIndex` is negative +```shell +Original string: Programming with Java +Substring from index 0 to 11: Programming +``` -## Example +In this example, `substring(0, 11)` extracts characters from index 0 (inclusive) to index 11 (exclusive). -The following example uses `.substring()` to display a substring of a given string. +## Frequently Asked Questions -```java -// Example.java -public class Example { - public static void main(String[] args) { - String blurb = "Java is cool!"; - // Printing last 5 characters of blurb - System.out.println(blurb.substring(blurb.length() - 5)); - // Printing first 4 characters of blurb - System.out.println(blurb.substring(0, 4)); - } -} -``` +### 1. Does substring start at 0 in Java? +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. -This produces the following output: +### 2. Is substring in Java inclusive? +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. -```shell -cool! -Java -``` +### 3. Is an empty string a substring? +Yes, an empty string is a valid substring in Java. You can get an empty string by: +- Using the same value for both `beginIndex` and `endIndex` in `substring(beginIndex, endIndex)` +- Using `substring(str.length())` which returns an empty string since it starts at the end of the string From 8465ddab4854aacf63243433b983cda161d21587 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Tue, 29 Apr 2025 00:00:31 +0530 Subject: [PATCH 2/5] Formating fix --- .../strings/terms/substring/substring.md | 122 +++++++++--------- 1 file changed, 63 insertions(+), 59 deletions(-) diff --git a/content/java/concepts/strings/terms/substring/substring.md b/content/java/concepts/strings/terms/substring/substring.md index 5e14e6bc48a..5da65be6400 100644 --- a/content/java/concepts/strings/terms/substring/substring.md +++ b/content/java/concepts/strings/terms/substring/substring.md @@ -13,98 +13,102 @@ CatalogContent: - 'paths/computer-science' --- -The **`.substring()`** method in Java 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. +The **`.substring()`** method in Java 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. -This method is useful for text processing, data parsing, and string manipulation tasks. There are two variants of this method that allow you to extract characters from a specific starting index or between a range of indices. +This method is useful for text processing, data parsing, and string manipulation tasks. There are two variants of this method that allow you to extract characters from a specific starting index or between a range of indices. -## Syntax +## Syntax -```pseudo -string.substring(startIndex); -string.substring(startIndex, endIndex); -``` +```pseudo +string.substring(startIndex); +string.substring(startIndex, endIndex); +``` -- `.substring()` returns characters from `startIndex` up to, but not including, the character at `endIndex`. +- `.substring()` returns characters from `startIndex` up to, but not including, the character at `endIndex`. -- If `endIndex` is omitted, `.substring()` returns characters from `startIndex` to the end of the string. +- If `endIndex` is omitted, `.substring()` returns characters from `startIndex` to the end of the string. -- If `startIndex` and `endIndex` are equal, `.substring()` returns an empty string. +- If `startIndex` and `endIndex` are equal, `.substring()` returns an empty string. -A [`StringIndexOutOfBoundsException`](https://www.codecademy.com/resources/docs/java/errors/stringindexoutofboundsexception) is thrown if any of the following are true: +A [`StringIndexOutOfBoundsException`](https://www.codecademy.com/resources/docs/java/errors/stringindexoutofboundsexception) is thrown if any of the following are true: -- `startIndex` is greater than `endIndex` -- `startIndex` is greater than the length of the original string -- `startIndex` or `endIndex` is negative +- `startIndex` is greater than `endIndex` +- `startIndex` is greater than the length of the original string +- `startIndex` or `endIndex` is negative -## Example 1: Extracting Characters From a Specific Index to the End +## Example 1: Extracting Characters From a Specific Index to the End -This example demonstrates how to extract a substring using `substring(beginIndex)` method starting from a specific index to the end of the string: +This example demonstrates how to extract a substring using `substring(beginIndex)` method starting from a specific index to the end of the string: -```java -class Main { +```java +class Main { - public static void main(String[] args) { + public static void main(String[] args) { - String text = "Hello, Java!"; + String text = "Hello, Java!"; - String result = text.substring(7); + String result = text.substring(7); - System.out.println("Original string: " + text); + System.out.println("Original string: " + text); - System.out.println("Substring starting from index 7: " + result); + System.out.println("Substring starting from index 7: " + result); - } -} -``` + } +} +``` -This example results in the following output: +This example results in the following output: -```shell -Original string: Hello, Java! -Substring starting from index 7: Java! -``` +```shell +Original string: Hello, Java! +Substring starting from index 7: Java! +``` -In this example, `substring(7)` extracts all characters from index 7 (inclusive) to the end of the string. +In this example, `substring(7)` extracts all characters from index 7 (inclusive) to the end of the string. -## Example 2: Extracting Characters Between Two Indices +## Example 2: Extracting Characters Between Two Indices -This example demonstrates how to extract a substring using `substring(beginIndex, endIndex)` method between a specific range of indices: +This example demonstrates how to extract a substring using `substring(beginIndex, endIndex)` method between a specific range of indices: -```java -class Main { +```java +class Main { - public static void main(String[] args) { + public static void main(String[] args) { - String text = "Programming with Java"; + String text = "Programming with Java"; - String result = text.substring(0, 11); + String result = text.substring(0, 11); - System.out.println("Original string: " + text); + System.out.println("Original string: " + text); - System.out.println("Substring from index 0 to 11: " + result); + System.out.println("Substring from index 0 to 11: " + result); - } -} -``` + } +} +``` -This example results in the following output: +This example results in the following output: -```shell -Original string: Programming with Java -Substring from index 0 to 11: Programming -``` +```shell +Original string: Programming with Java +Substring from index 0 to 11: Programming +``` -In this example, `substring(0, 11)` extracts characters from index 0 (inclusive) to index 11 (exclusive). +In this example, `substring(0, 11)` extracts characters from index 0 (inclusive) to index 11 (exclusive). -## Frequently Asked Questions +## Frequently Asked Questions -### 1. Does substring start at 0 in Java? -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. +### 1. Does substring start at 0 in Java? -### 2. Is substring in Java inclusive? -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. +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. -### 3. Is an empty string a substring? -Yes, an empty string is a valid substring in Java. You can get an empty string by: -- Using the same value for both `beginIndex` and `endIndex` in `substring(beginIndex, endIndex)` -- Using `substring(str.length())` which returns an empty string since it starts at the end of the string +### 2. Is substring in Java inclusive? + +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. + +### 3. Is an empty string a substring? + +Yes, an empty string is a valid substring in Java. You can get an empty string by: + +- Using the same value for both `beginIndex` and `endIndex` in `substring(beginIndex, endIndex)` +- Using `substring(str.length())` which returns an empty string since it starts at the end of the string From 0112e62b54da5880c37417966c7262bb5008e65a Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Tue, 29 Apr 2025 00:02:25 +0530 Subject: [PATCH 3/5] minor changes --- .../strings/terms/substring/substring.md | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/content/java/concepts/strings/terms/substring/substring.md b/content/java/concepts/strings/terms/substring/substring.md index 5da65be6400..1544846143d 100644 --- a/content/java/concepts/strings/terms/substring/substring.md +++ b/content/java/concepts/strings/terms/substring/substring.md @@ -24,10 +24,8 @@ string.substring(startIndex); string.substring(startIndex, endIndex); ``` -- `.substring()` returns characters from `startIndex` up to, but not including, the character at `endIndex`. - +- `.substring()` returns characters from `startIndex` up to, but does not include, the character at `endIndex`. - If `endIndex` is omitted, `.substring()` returns characters from `startIndex` to the end of the string. - - If `startIndex` and `endIndex` are equal, `.substring()` returns an empty string. A [`StringIndexOutOfBoundsException`](https://www.codecademy.com/resources/docs/java/errors/stringindexoutofboundsexception) is thrown if any of the following are true: @@ -38,13 +36,13 @@ A [`StringIndexOutOfBoundsException`](https://www.codecademy.com/resources/docs/ ## Example 1: Extracting Characters From a Specific Index to the End -This example demonstrates how to extract a substring using `substring(beginIndex)` method starting from a specific index to the end of the string: +This example demonstrates how to extract a substring using `substring(beginIndex)` method, starting from a specific index to the end of the string: ```java class Main { - + public static void main(String[] args) { - + String text = "Hello, Java!"; String result = text.substring(7); @@ -98,11 +96,11 @@ In this example, `substring(0, 11)` extracts characters from index 0 (inclusive) ## Frequently Asked Questions -### 1. Does substring start at 0 in Java? +### 1. Does a substring start at 0 in Java? 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. -### 2. Is substring in Java inclusive? +### 2. Is a substring in Java inclusive? 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. @@ -111,4 +109,4 @@ The `beginIndex` parameter in the `substring()` method is inclusive, meaning the Yes, an empty string is a valid substring in Java. You can get an empty string by: - Using the same value for both `beginIndex` and `endIndex` in `substring(beginIndex, endIndex)` -- Using `substring(str.length())` which returns an empty string since it starts at the end of the string +- Using `substring(str.length())`, which returns an empty string since it starts at the end of the string From 4cd43d8059f0a4a5e51cef96edf6f73724dde1b3 Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Tue, 29 Apr 2025 00:03:39 +0530 Subject: [PATCH 4/5] Update substring.md --- content/java/concepts/strings/terms/substring/substring.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/content/java/concepts/strings/terms/substring/substring.md b/content/java/concepts/strings/terms/substring/substring.md index 1544846143d..e6f53390431 100644 --- a/content/java/concepts/strings/terms/substring/substring.md +++ b/content/java/concepts/strings/terms/substring/substring.md @@ -13,9 +13,9 @@ CatalogContent: - 'paths/computer-science' --- -The **`.substring()`** method in Java 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. +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. -This method is useful for text processing, data parsing, and string manipulation tasks. There are two variants of this method that allow you to extract characters from a specific starting index or between a range of indices. +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. ## Syntax From 6b948f2fb06630c8ee536c853b8a6a3be75ce04e Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Tue, 29 Apr 2025 00:06:53 +0530 Subject: [PATCH 5/5] formating fix --- content/java/concepts/strings/terms/substring/substring.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/content/java/concepts/strings/terms/substring/substring.md b/content/java/concepts/strings/terms/substring/substring.md index e6f53390431..a6fe2250d24 100644 --- a/content/java/concepts/strings/terms/substring/substring.md +++ b/content/java/concepts/strings/terms/substring/substring.md @@ -40,9 +40,9 @@ This example demonstrates how to extract a substring using `substring(beginIndex ```java class Main { - + public static void main(String[] args) { - + String text = "Hello, Java!"; String result = text.substring(7);