import java.io.*;
public class stringBuffer
{
public static void main(String[] args) throws Exception
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String str;
try
{
//String
String s=new String("Welcome");
//length
System.out.println("String Length: "+s.length());
//concat
s=s.concat(" to BDU");
System.out.println("New String "+ s);
//charat
System.out.println("Character at 2nd position: "+s.charAt(2));
//Substring
System.out.println("Sub String: "+ s.substring(0,5));
//String Buffer
StringBuffer strbuf=new StringBuffer();
//append
strbuf.append("Hello");
strbuf.append("World");
System.out.println(strbuf);
//insert
strbuf.insert(5,"_Java ");
System.out.println(strbuf);
//reverse
strbuf.reverse();
System.out.print("Reversed string : ");
System.out.println(strbuf);
strbuf.reverse();
System.out.println(strbuf);
//setCharAt
strbuf.setCharAt(5,' ');
System.out.println(strbuf);
//charAt
System.out.print("Character at 6th position : ");
System.out.println(strbuf.charAt(6));
//substring
System.out.print("Substring from position 3 to 6 : ");
System.out.println(strbuf.substring(3,7));
//deleteCharAt
strbuf.deleteCharAt(3);
System.out.println(strbuf);
//capacity
System.out.print("Capacity of StringBuffer object : ");
System.out.println(strbuf.capacity());
}
catch(StringIndexOutOfBoundsException e)
{
System.out.println(e.getMessage());
}
}
}
No comments:
Post a Comment