Java: Extract the first half of a string of even length
Extract First Half
Write a Java program to extract the first half of a even string.
Pictorial Presentation:
Sample Solution:
Java Code:
import java.lang.*;
public class Exercise69 {
public static void main(String[] args) {
// Define the main string
String main_string = "Python";
// Extract the substring from the beginning to the middle of the string
String substring = main_string.substring(0, main_string.length() / 2);
// Print the extracted substring
System.out.println(substring);
}
}
Sample Output:
Pyt
Flowchart:
For more Practice: Solve these Related Problems:
- Modify the program to extract the second half of the string.
- Write a program to extract the middle character(s) of a string.
- Modify the program to extract the first quarter of the string.
- Write a program to extract the first half of each word in a sentence.
Go to:
PREV : Repeat Last 3 Characters.
NEXT :
Short + Long + Short String.
Java Code Editor:
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.