Java.lang - SPLessons

Java.lang String Buffer

Home > Lesson > Chapter 11
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.lang String Buffer

Java.lang String Buffer

shape Description

String is an object in Java programming language, Java itself provided String class to manipulate Strings. The java.lang.String is the class to support more methods on a string, these methods are used to execute operations on a string like comparing, converting, replacing, trimming. The java.lang.String class implements some interfaces such as Serializable, Comparable and CharSequence interfaces. Following is the syntax declaration of java.lang String class. [java]public final class String extends Object implements Serializable, Comparable<String>, CharSequence [/java] The java.lang.StringBuffer class is a string sheltered, alterable succession of characters. Taking after are the vital focuses about StringBuffer. Taking after is the announcement for java.lang.StringBuffer class. [java]public final class StringBuffer extends Object implements Serializable, CharSequence[/java]

Methods Of Java.lang String Buffer

shape Description

The following are the various important methods of the class.

StringBuffer append(boolean b)

The java.lang.StringBuffer.append(boolean b) strategy attaches the string representation of the boolean contention to the succession. Following is the declaration for java.lang.StringBuffer.append() method. [java]public StringBuffer append(boolean b)[/java] The following example shows the usage of java.lang.StringBuffer.append() method. [java]import java.lang.*; public class StringBufferDemo { public static void main(String[] args) { StringBuffer buff = new StringBuffer("tuts "); System.out.println("buffer = " + buff); // appends the boolean argument as string to the string buffer buff.append(true); // print the string buffer after appending System.out.println("After append = " + buff); buff = new StringBuffer("abcd "); System.out.println("buffer = " + buff); // appends the boolean argument as string to the string buffer buff.append(false); // print the string buffer after appending System.out.println("After append = " + buff); } }[/java] The result will be as follows. [java]buffer = tuts After append = tuts true buffer = abcd After append = abcd false[/java]

int capacity()

The java.lang.StringBuffer.capacity() strategy gives back the present limit. The limit is the measure of capacity accessible for recently embedded characters, past which an allotment will happen. Following is the declaration for java.lang.StringBuffer.capacity() method. [java]public int capacity()[/java] The following example shows the usage of java.lang.StringBuffer.capacity() method. [java]import java.lang.*; public class StringBufferDemo { public static void main(String[] args) { StringBuffer buff = new StringBuffer("splessons"); // returns the current capacity of the String buffer i.e. 16 + 14 System.out.println("capacity = " + buff.capacity()); buff = new StringBuffer(" "); // returns the current capacity of the String buffer i.e. 16 + 1 System.out.println("capacity = " + buff.capacity()); } }[/java] Now the result will be as follows. [java]capacity = 25 capacity = 17 [/java]

int codePointAt(int index)

The java.lang.StringBuffer.codePointAt() method returns the character (Unicode code point) at the specified index.The index refers to char values (Unicode code units) and ranges from 0 to length() - 1. The following example shows the usage of java.lang.StringBuffer.codePointAt() method. [java]import java.lang.*; public class StringBufferDemo { public static void main(String[] args) { StringBuffer buff = new StringBuffer("TUTORIALS"); System.out.println("buffer = " + buff); // returns the codepoint at index 5 int retval = buff.codePointAt(5); System.out.println("Character(unicode point) = " + retval); buff = new StringBuffer("amrood admin "); System.out.println("buffer = " + buff); // returns the codepoint at index 6 i.e whitespace character retval = buff.codePointAt(6); System.out.println("Character(unicode point) = " + retval); } }[/java] Now the result will be as follows. [java]buffer = TUTORIALS Character(unicode point) = 73 buffer = amrood admin Character(unicode point) = 32 [/java]

StringBuffer reverse()

The reverse() method causes this character sequence to be replaced by the reverse of the sequence. Following is the declaration for java.lang.StringBuffer.reverse() method. [java]public StringBuffer reverse()[/java] The following example shows the usage of java.lang.StringBuffer.reverse() method. [java]import java.lang.*; public class StringBufferDemo { public static void main(String[] args) { StringBuffer buff = new StringBuffer("splesson"); System.out.println("buffer = " + buff); // reverse characters of the buffer and prints it System.out.println("reverse = " + buff.reverse()); // reverse of the buffer is equivalent to the actual buffer buff = new StringBuffer("hello"); System.out.println("buffer = " + buff); // reverse characters of the buffer and prints it System.out.println("reverse = " + buff.reverse()); } }[/java] Now the result will be as follows. [java] buffer = splesson reverse = nosselps buffer = hello reverse = olleh [/java]

Summary

shape Key Points

  • The StringBuffer delete(int start, int end) method removes the characters in a substring of this sequence.
  • The capacity() method returns the current capacity.