-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStringMethods.java
More file actions
62 lines (43 loc) · 1.64 KB
/
StringMethods.java
File metadata and controls
62 lines (43 loc) · 1.64 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import java.util.Scanner;
/**
* 35. A simple Java program to: 1. Find the length of a string 2. Replace part
* of a string 3. Concatenate another string
*/
public class StringMethods {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
try {
System.out.println("==================================");
System.out.println(" String Methods Java Program ");
System.out.println("==================================");
// Input main string
System.out.print("Enter a string: ");
String input = scanner.nextLine().trim();
// Find string
System.out.print("Enter the word to replace: ");
String findString = scanner.nextLine().trim();
// Replace string
System.out.print("Enter the replacement word: ");
String replaceString = scanner.nextLine().trim();
// Concatenate string
System.out.print("Enter a string to concatenate: ");
String concatenateString = scanner.nextLine().trim();
// Find length
int length = input.length();
// Replace text
String replacedString = input.replace(findString, replaceString);
// Concatenate text
String concatenatedString = input.concat(" " + concatenateString);
// Output results
System.out.println("\n========= OUTPUT =========");
System.out.println("Original String : " + input);
System.out.println("String Length : " + length);
System.out.println("Replaced String : " + replacedString);
System.out.println("Concatenated String : " + concatenatedString);
} catch (Exception e) {
System.out.println("An unexpected error occurred: " + e.getMessage());
} finally {
scanner.close();
}
}
}