0% found this document useful (0 votes)
3 views

java 7a,b

The document provides two Java programs demonstrating the use of String and StringBuffer class methods. The first program showcases various String methods such as charAt, concat, equals, length, replace, substring, and others. The second program illustrates StringBuffer methods including append, insert, replace, delete, reverse, setCharAt, ensureCapacity, trimToSize, and setLength.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

java 7a,b

The document provides two Java programs demonstrating the use of String and StringBuffer class methods. The first program showcases various String methods such as charAt, concat, equals, length, replace, substring, and others. The second program illustrates StringBuffer methods including append, insert, replace, delete, reverse, setCharAt, ensureCapacity, trimToSize, and setLength.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

7. a)Write a Java program to demonstrate String class methods.

Source
Code:
// Lab Prg:7 W.A.P to demonstrate on using String Class methods
import java.lang.*;
class StringMethods
{ public static void main(String[] args)
{
String targetString = "Java is fun to learn";
String s1= "JAVA";
String s2= "Java";
String s3 = " Hello Java ";

System.out.println("Char at index 2(third position): " +


targetString.charAt(2));
System.out.println("After Concat: "+ targetString.concat("-Enjoy-"));
System.out.println("Checking equals ignoring case: "
+s2.equalsIgnoreCase(s1));
System.out.println("Checking equals with case: " +s2.equals(s1));
System.out.println("Checking Length: "+ targetString.length());
System.out.println("Replace function: "+ targetString.replace("fun", "easy"));
System.out.println("SubString of targetString: "+ targetString.substring(8));
System.out.println("SubString of targetString: "+ targetString.substring(8,
12));
System.out.println("Converting to lower case: "+
targetString.toLowerCase());
System.out.println("Converting to upper case: "+ targetString.toUpperCase());
System.out.println("Triming string: " + s3.trim());
System.out.println("searching s1 in targetString: " + targetString.contains(s1));
System.out.println("searching s2 in targetString: "
+ targetString.contains(s2));
char [] charArray = s2.toCharArray();
System.out.println("Size of char array: " + charArray.length);
System.out.println("Printing last element of array: " + charArray[3]);

}
Output:
7.b)Write a Java program to demonstrate String Buffer class methods.
import java.lang.*;
public classStringBufferExample
{
public static void main(String[]args) {
// Create a StringBuffer object
StringBuffer sb = new StringBuffer("Hello");

// 1.Append a string
sb.append(" World");
System.out.println("After append: " + sb);

// 2.Insert a string at a specified index


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

//3. Replace a portion of the string


sb.replace(5, 6, "!");
System.out.println("After replace: " + sb);

// 4.Delete a portion of the string


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

//5. Reverse the string


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

// 6.Set a new character at a specific index


sb.setCharAt(0, 'h');
System.out.println("After setCharAt: " + sb);

//7. Ensure the capacity of the StringBuffer


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

//8. Trim the capacity to the current length


sb.trimToSize();
System.out.println("Capacity after trimToSize: " + sb.capacity());
//9. Get the length of the StringBuffer
int length = sb.length();
System.out.println("Length: " + length);

// 10.Get the current string from the StringBuffer


String currentString = sb.toString();
System.out.println("StringBuffer as String: " + currentString);

// 11.Replace the entire string (using setLength)


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

You might also like