Java Online Compiler
Example: SubstringExtraction in Java
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// SubstringExtraction public class Main { public static void main(String[] args) { // Step 1: Initialize a string String sentence = "The quick brown fox"; // Step 2: Extract a substring from a specific starting index to the end // substring(startIndex) - extracts from startIndex (inclusive) to the end String part1 = sentence.substring(4); // "quick brown fox" // Step 3: Extract a substring from a starting index to an ending index // substring(startIndex, endIndex) - extracts from startIndex (inclusive) to endIndex (exclusive) String part2 = sentence.substring(4, 9); // "quick" // Step 4: Print the original and extracted substrings System.out.println("Original sentence: \"" + sentence + "\""); System.out.println("Substring from index 4 to end: \"" + part1 + "\""); System.out.println("Substring from index 4 to 9: \"" + part2 + "\""); } }
Output
Clear
ADVERTISEMENTS