Download Java StringBuffer and StringBuilder Methods and more Slides Java Programming in PDF only on Docsity!
String Buffer
append( )
The append( ) method concatenates the string representation of any
other type of data to the end of the invoking StringBuffer object. It has
overloaded versions for all the built-in types and for Object. Here are a
few of its forms:
StringBuffer append(String str)
StringBuffer append(int num)
StringBuffer append(Object obj)
String.valueOf( ) is called for each parameter to obtain its string
representation. The result is appended to the current StringBuffer object.
The buffer itself is returned by each version of append( ). This allows
subsequent calls to be chained together.
adrish.b@ardentcollaboratio
176
String Buffer
Program showing
append()
class appendDemo { public static void main(String args[]) { String s; int a = 42; StringBuffer sb = new StringBuffer(40); s = sb.append("a=").append(a).append("!").toString(); System.out.println(s); } }
adrish.b@ardentcollaboratio
177
a = 42!
String Buffer
insert( ) class insertDemo {
public static void main(String args[]) {
StringBuffer sb = new StringBuffer("I
Java!");
sb.insert(2, "like ");
System.out.println(sb);
adrish.b@ardentcollaboratio
179
I like Java!
String Buffer
reverse( )
You can reverse the characters within a StringBuffer object using reverse( ),
shown here:
StringBuffer reverse( )
This method returns the reversed object on which it was called.
adrish.b@ardentcollaboratio
180
String Buffer
delete( ) and deleteCharAt( )
Used to delete chararcters from StringBuffer.
Forms:
StringBuffer delete(int startIndex, int endIndex)
StringBuffer deleteCharAt(int loc)
adrish.b@ardentcollaboratio
182
String Buffer
delete()
and
deleteCharAt()
class deleteDemo {
public static void main(String args[]) {
StringBuffer sb = new StringBuffer("This is a test.");
sb.delete(4, 7);
System.out.println("After delete: " + sb);
sb.deleteCharAt(0);
System.out.println("After deleteCharAt: " + sb);
adrish.b@ardentcollaboratio
183
After delete: This a test. After deleteCharAt: his a test.
String Buffer
repalce() class replaceDemo {
public static void main(String args[]) {
StringBuffer sb = new StringBuffer("This is a
test.");
sb.replace(5, 7, "was");
System.out.println("After replace: " + sb);
adrish.b@ardentcollaboratio
185
After replace: This was a test.
String Buffer
substring( )
Java also added the substring( ) method, which returns a
portion of a StringBuffer. It has the following two
forms:
String substring(int startIndex)
String substring(int startIndex, int endIndex)
The first form returns the substring that starts at startIndex and
runs to the end of the invoking StringBuffer object. The second
form returns the substring that starts at startIndex and runs
through endIndex–1. These methods work just like those defined
for String that were described earlier.
adrish.b@ardentcollaboratio
186
(^188) Explore java.lang package
adrish.b@ardentcollaboratio
By Adrish Bhattacharyay Ardent Collaborations
Introduction
java.lang is automatically imported into all programs. It
contains classes and interfaces that are fundamental to
virtually all of Java programming. It is Java‘s most widely
used package.
adrish.b@ardentcollaboratio
189
java.lang.*(interfaces)
Appendable Comparable Runnable CharSequence Iterable Cloneable Readable
adrish.b@ardentcollaboratio
191
Primitive Type Wrapper
Number
The abstract class Number defines a superclass that is implemented by the classes
that wrap the numeric types byte, short, int, long, float, and double. Number has
abstract methods that return the value of the object in each of the different number
formats.That is, doubleValue( ) returns the value as a double, floatValue( ) returns
the value as a float, and so on. These methods are shown here:
The values returned by these methods can be rounded. Number has six concrete
subclasses that hold explicit values of each numeric type:
adrish.b@ardentcollaboratio
192
byte byteValue( ) double doubleValue( ) float floatValue( )
int intValue( ) long longValue( ) short shortValue( )
Double Float Byte Short Integer Long
Primitive Type Wrapper
Number
Double and Float:
The constructors for Double are shown here:
Double(double num)
Double(String str) throws NumberFormatException
Double objects can be constructed with a double value or a string containing a
floating-point value.
adrish.b@ardentcollaboratio
194
Primitive Type Wrapper
Number
Double and Float: Both Float and Double define the following constants:
adrish.b@ardentcollaboratio
195
MAX_VALUE Maximum positive value
MIN_VALUE Minimum positive value
NaN Not a number
POSITIVE_INFINITY Positive infinity
NEGATIVE_INFINITY Negative infinity
TYPE The Class object for float or double
SIZE The bit width of the wrapped value(from J2SE5)
MAX_EXPONENT Maximum exponent(from JSE6)
MIN_EXPONENT Minimum exponent(from JSE6)
MIN_NORMAL Minimum positive normal value(from JSE6)