-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringOperation.java
More file actions
44 lines (31 loc) · 1.38 KB
/
StringOperation.java
File metadata and controls
44 lines (31 loc) · 1.38 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
35
36
37
38
39
40
41
42
43
44
import java.util.Scanner;
public class StringOperation {
public static void main(String[] args) {
try(Scanner scan = new Scanner(System.in)){
System.out.println("Enter the first string:");
String str1 = scan.nextLine();
System.out.println("Enter the second string:");
String str2 = scan.nextLine();
String concatenated = str1 + " " + str2;
System.out.println("Concatenated string\n" + concatenated);
System.out.println("Enter a substring to search: ");
String substr = scan.nextLine();
if (concatenated.contains(substr)) {
System.out.println("Substring found!");
}
else{System.out.println("Substring not found!");}
System.out.println("Enter start index: ");
int start = scan.nextInt();
System.out.println("Enter end index: ");
int end = scan.nextInt();
if (start<end && start>0 && end<concatenated.length() && end>0) {
String extString = concatenated.substring(start,end);
System.out.println("Extracted substring is\n" + extString);
}
else{System.out.println("Invalid indices!");}
}
catch(Exception e){
System.out.println("Please Enter String as Input!");
}
}
}