[go: up one dir, main page]

0% found this document useful (0 votes)
8 views2 pages

EXP 4 java

The document contains two Java programs demonstrating the use of the String and StringBuffer classes. The first program showcases various String methods such as length, substring, and replace, while the second program illustrates StringBuffer methods including append, insert, and reverse. Both programs print the results of the operations performed on the respective string objects.

Uploaded by

hshzjbzbs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

EXP 4 java

The document contains two Java programs demonstrating the use of the String and StringBuffer classes. The first program showcases various String methods such as length, substring, and replace, while the second program illustrates StringBuffer methods including append, insert, and reverse. Both programs print the results of the operations performed on the respective string objects.

Uploaded by

hshzjbzbs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Aashay Kulkarni 183 SYCO c

Exp 4

1. Write a program to show the use of all methods of String class.

public class StringMethodsDemo {

public static void main(String[] args) {

String str = "Hello, World!";

String str2 = " Java Programming ";

System.out.println("1. Length: " + str.length());

System.out.println("2. Character at index 1: " + str.charAt(1));

System.out.println("3. Substring from index 7: " + str.substring(7));

System.out.println("4. Contains 'World': " + str.contains("World"));

System.out.println("5. Equals 'hello, world!': " + str.equals("hello, world!"));

System.out.println("6. Starts with 'Hello': " + str.startsWith("Hello"));

System.out.println("7. Index of 'o': " + str.indexOf('o'));

System.out.println("8. Replace 'World' with 'Java': " + str.replace("World", "Java"));

System.out.println("9. Lowercase: " + str.toLowerCase());

System.out.println("10. Trimmed String: '" + str2.trim() + "'");

}
Aashay Kulkarni 183 SYCO c

2. Write a program to implement all methods of StringBuffer class.

public class StringBufferDemo {

public static void main(String[] args) {

StringBuffer sb = new StringBuffer("Hello");

sb.append(", World!");

System.out.println("1. After append: " + sb);

sb.insert(5, " Java");

System.out.println("2. After insert: " + sb);

sb.replace(6, 10, "Python");

System.out.println("3. After replace: " + sb);

sb.delete(6, 12);

System.out.println("4. After delete: " + sb);

sb.reverse();

System.out.println("5. After reverse: " + sb);

sb.reverse();

sb.setCharAt(0, 'h');

System.out.println("6. After setCharAt: " + sb);

System.out.println("7. Buffer Capacity: " + sb.capacity());

System.out.println("8. Length: " + sb.length());

System.out.println("9. Substring from index 2: " + sb.substring(2));

sb.setLength(5);

System.out.println("10. After setLength(5): " + sb);

You might also like