In this tutorial you will learn about Java StringBuffer class
String Buffer Class
java.lang.StringBuffer class is similar to the java.lang.String class but it holds the mutable sequence of character. Mutable means the objects of the class can be modified rather than creating new one. The StringBuffer class is thread safe and its methods are also synchronized, it implements the Serializable interface. It has numbers of methods to for manipulation of its object.
An example of StringBuffer class is given below with its commonly used methods.
StringBufferExample.java
package net.roseindia; public class StringBufferExample { public static void main(String[] args) { StringBuffer stringBuffer = new StringBuffer("Java"); System.out.println(stringBuffer); /* append() */ stringBuffer.append(" Pro"); System.out.println(stringBuffer); /* insert() */ stringBuffer.insert(8, "gramming"); System.out.println(stringBuffer); /* reverse() */ stringBuffer.reverse(); System.out.println(stringBuffer); /* charAt() */ char c = stringBuffer.charAt(4); System.out.println(c); /* setCharAt() */ stringBuffer.setCharAt(5, 'c'); System.out.println(stringBuffer); /* substring() */ String newString = stringBuffer.substring(1, 5); System.out.println(newString); /* deleteCharAt() */ stringBuffer.deleteCharAt(4); System.out.println(stringBuffer); /* length() */ int stringLength = stringBuffer.length(); System.out.println(stringLength); /* delete() */ stringBuffer.delete(0, 11); System.out.println(stringBuffer); /* capacity() */ stringBuffer.capacity(); System.out.println(stringBuffer.capacity()); } }
When you run this application it will display message as shown below:
Java Java Pro Java Programming gnimmargorP avaJ m gnimmcrgorP avaJ nimm gnimcrgorP avaJ 15 avaJ 20 |
Download Source Code
[ 0 ] Comments