Documentos de Académico
Documentos de Profesional
Documentos de Cultura
import java.util.Scanner;
public class StringBufferExample {
public static void main(String[] args) {
Scanner str = new Scanner(System.in);
System.out.print("Enter your name: ");
StringBuffer strbuf = new StringBuffer( str.nextLine()) ;
strbuf.append(",This is the example of SringBuffer class and it's functions.");
System.out.println(strbuf);
strbuf.delete(0,strbuf.length());
strbuf.append("Hello");
strbuf.append("World");
System.out.println(strbuf);
strbuf.insert(5,"_Java ");
System.out.println(strbuf);
strbuf.reverse();
System.out.print("Reversed string : ");
System.out.println(strbuf);
strbuf.reverse();
System.out.println(strbuf);
strbuf.setCharAt(5,' ');
System.out.println(strbuf);
System.out.print("Character at 6th position : ");
System.out.println(strbuf.charAt(6));
System.out.print("Substring from position 3 to 6 : ");
System.out.println(strbuf.substring(3,7));
strbuf.deleteCharAt(3);
System.out.println(strbuf);
System.out.print("Capacity of StringBuffer object : ");
System.out.println(strbuf.capacity());
strbuf.delete(6,strbuf.length());
System.out.println(strbuf);
}
}
run:
Enter your name: Juan Vicente
Juan Vicente,This is the example of SringBuffer class and it's functions.
HelloWorld
Hello_Java World
Reversed string : dlroW avaJ_olleH
Hello_Java World
Hello Java World
Character at 6th position : J
Substring from position 3 to 6 : lo J
Helo Java World
Capacity of StringBuffer object : 73
Helo J
BUILD SUCCESSFUL (total time: 8 seconds)