Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

StringBuffer.java

The document is a Java program demonstrating various methods of the StringBuffer class. It showcases operations such as appending, inserting, replacing, deleting, reversing, and manipulating the capacity and length of a StringBuffer object. Each method is illustrated with examples and output statements to show the results of the operations.
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

StringBuffer.java

The document is a Java program demonstrating various methods of the StringBuffer class. It showcases operations such as appending, inserting, replacing, deleting, reversing, and manipulating the capacity and length of a StringBuffer object. Each method is illustrated with examples and output statements to show the results of the operations.
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 2

public class StringBufferMethodsDemo {

public static void main(String[] args) {

// 1. Creating a StringBuffer object

StringBuffer sb = new StringBuffer("Hello");

// 2. append() - Adds text at the end

sb.append(" Java");

System.out.println("Append: " + sb);

// 3. insert() - Inserts text at a specific position

sb.insert(6, "World ");

System.out.println("Insert: " + sb);

// 4. replace() - Replaces characters in a specific range

sb.replace(6, 11, "Beautiful");

System.out.println("Replace: " + sb);

// 5. delete() - Deletes characters in a specific range

sb.delete(6, 15);

System.out.println("Delete: " + sb);

// 6. deleteCharAt() - Deletes a character at a specific index

sb.deleteCharAt(5);

System.out.println("DeleteCharAt: " + sb);

// 7. reverse() - Reverses the string

sb.reverse();

System.out.println("Reverse: " + sb);

sb.reverse(); // Reverting back to original for further operations

// 8. capacity() - Returns the current capacity

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

// 9. ensureCapacity() - Ensures minimum capacity

sb.ensureCapacity(50);

System.out.println("Capacity after ensuring 50: " + sb.capacity());

// 10. length() - Returns the length of the string

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

// 11. charAt() - Returns a character at a specific index


System.out.println("Char at index 1: " + sb.charAt(1));

// 12. setCharAt() - Sets a character at a specific index

sb.setCharAt(1, 'a');

System.out.println("SetCharAt: " + sb);

// 13. substring(start) - Extracts substring from start index

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

// 14. substring(start, end) - Extracts substring from start to end-1

System.out.println("Substring (0,5): " + sb.substring(0, 5));

// 15. indexOf() - Returns index of first occurrence of a string

System.out.println("Index of 'Java': " + sb.indexOf("Java"));

// 16. lastIndexOf() - Returns index of last occurrence of a string

sb.append(" Java is fun");

System.out.println("Last index of 'Java': " + sb.lastIndexOf("Java"));

// 17. setLength() - Sets the length of the string

sb.setLength(5);

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

You might also like