-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringBufferOperations.java
More file actions
29 lines (28 loc) · 1.1 KB
/
StringBufferOperations.java
File metadata and controls
29 lines (28 loc) · 1.1 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
import java.util.Scanner;
public class StringBufferOperations {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a string:");
String input = scanner.nextLine();
StringBuffer sb = new StringBuffer(input);
// Length of string
int length = sb.length();
System.out.println("Length of the string: " + length);
// Reverse a string
StringBuffer reversedString = sb.reverse();
System.out.println("Reversed string: " + reversedString);
sb.reverse();
// Delete a substring from the given string
System.out.println("Enter the substring to delete:");
String substringToDelete = scanner.nextLine();
int start = sb.indexOf(substringToDelete);
if (start != -1) {
int end = start + substringToDelete.length();
sb.delete(start, end);
System.out.println("String after deletion: " + sb);
} else {
System.out.println("Substring not found in the string.");
}
scanner.close();
}
}