Vous êtes sur la page 1sur 3

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

import java.util.*;
class alll
{
public static void main(String args[])
{
String a,a1;
String a2 = new String("Saahil");
Scanner s = new Scanner(System.in);
System.out.println("Enter String = ");
a=s.next();

System.out.println("Length = " +a.length());

System.out.println("Lower = "+a.toLowerCase());

System.out.println("Upper = "+a.toUpperCase());

System.out.println("Replace = "+a.replace('s','b'));

System.out.println("Trim = "+a.trim());

System.out.println("Enetr second String = ");


a1=s.next();

if(a.equals(a1))
{
System.out.println("Equal/Not Equal = Equal");
}
else
{
System.out.println("Equal/Not Equal = Not Equal");
}

if(a.equalsIgnoreCase(a1))
{
System.out.println("EqualIgnoreCase = Equal");
}
else
{
System.out.println("EqualIgnoreCase = Not equals");
}

System.out.println("CharAt = "+a.charAt(5));

System.out.println("CompareTo = "+a.compareTo(a1));

System.out.println("Concat = "+a.concat(a1));

System.out.println("SubString = "+a.substring(4));

System.out.println("SubString = "+a.substring(2,4));
System.out.println("IndexOf = "+a2.indexOf("a"));

}
}

Output:

11.2] Write a program to show the use of all methods of String Buffer class.
class strb
{
public static void main(String args[])
{
StringBuffer sb = new StringBuffer("Nesp");
sb.setCharAt(2,'p');
System.out.println("CharAt = "+sb);

StringBuffer sb1 = new StringBuffer("Nesp Bhandup");


sb1.append(" Department Co");
System.out.println("Append = "+sb1);

StringBuffer sb2 = new StringBuffer("Nesp Bhandp");


sb2.insert(4," co.dept");
System.out.println("Insert = "+sb2);

StringBuffer sb3 = new StringBuffer("Nesp");


sb3.reverse();
System.out.println("Reverse = "+sb3);

StringBuffer sb4 = new StringBuffer("Nesp");


sb4.delete(1,2);
System.out.println("Delete = "+sb4);
StringBuffer sb5 = new StringBuffer("Nesp");
sb5.deleteCharAt(2);
System.out.println("DeleteCharAt = "+sb5);

}
}

Output:

Vous aimerez peut-être aussi