-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringOperations.java
More file actions
34 lines (33 loc) · 1.62 KB
/
StringOperations.java
File metadata and controls
34 lines (33 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.util.Scanner;
public class StringOperations {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// String concatenation
System.out.println("Enter the first string for concatenation:");
String str1 = scanner.nextLine();
System.out.println("Enter the second string for concatenation:");
String str2 = scanner.nextLine();
String concatenatedString = str1.concat(str2);
System.out.println("Concatenated string:" + concatenatedString);
// Search a substring
System.out.println("Enter a string to search:");
String searchstring = scanner.nextLine();
if (concatenatedString.contains(searchstring)) {
System.out.println("Substring ' " + searchstring + " ' found in the concatenated string");
} else {
System.out.println("Substring '" + searchstring + "' not found in the concatenated string");
}
// Extract substring
System.out.println("Enter the starting index to extract substring:");
int startIndex = scanner.nextInt();
System.out.println("Enter the ending index to extract substring:");
int endIndex = scanner.nextInt();
if (startIndex >= 0 && startIndex < endIndex && endIndex <= concatenatedString.length()) {
String extractedSubstring = concatenatedString.substring(startIndex, endIndex);
System.out.println("Extracted substring:" + extractedSubstring);
} else {
System.out.println("Invalid indices provided. Extraction failed.");
}
scanner.close();
}
}