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

Shaa 4

The document is a Java program demonstrating various string and StringBuffer operations. It showcases methods such as charAt, length, substring, toUpperCase, and more on a String object, as well as append, insert, delete, replace, and reverse on a StringBuffer object. The program also includes capacity management for the StringBuffer.

Uploaded by

shanne724
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)
4 views2 pages

Shaa 4

The document is a Java program demonstrating various string and StringBuffer operations. It showcases methods such as charAt, length, substring, toUpperCase, and more on a String object, as well as append, insert, delete, replace, and reverse on a StringBuffer object. The program also includes capacity management for the StringBuffer.

Uploaded by

shanne724
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

public class StringAndStringBufferExample {

public static void main(String[] args) {

String str1 = " Shannel Dcosta ";

System.out.println("charAt(1): " + str1.charAt(1));

System.out.println("length(): " + str1.length());

System.out.println("substring(1, 5): " + str1.substring(1, 5));

System.out.println("toUpperCase(): " + str1.toUpperCase());

System.out.println("toLowerCase(): " + str1.toLowerCase());

System.out.println("trim(): '" + str1.trim() + "'");

System.out.println("replace('Shannel', 'Java'): " + str1.replace("World", "Java"));

System.out.println("concat('!!!'): " + str1.concat("!!!"));

System.out.println("contains('Shannel'): " + str1.contains("Shannel"));

System.out.println("indexOf('Shannel'): " + str1.indexOf("Shannel"));

StringBuffer sb = new StringBuffer("Shannel");

sb.append("!!!");

System.out.println("append('!!!'): " + sb);

sb.insert(6, "Java ");

System.out.println("insert(6, 'xie '): " + sb);

sb.delete(6, 11);

System.out.println("delete(6, 11): " + sb);

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

System.out.println("replace(6, 11, 'xie'): " + sb);

sb.reverse();

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

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


sb.ensureCapacity(50);

System.out.println("capacity after ensureCapacity(50): " + sb.capacity()); }

You might also like