Strings
Strings
Note: Static methods will be called using class name and non static
methods using object reference.
String Class Methods
• Note: The above explained methods are the most common methods, but there are other
methods too for which one can refer to official String class documentation by Oracle.
StringBuffer Class
• StringBuffer class under strings in java is used to create mutable and
thread-safe (multiple threads can’t access it simultaneously) string
object.
• Strings in java are created using StringBuffer can be changed unlike
String class objects.
• Use StringBuffer when you want to make modifications in strings and
want your object to be thread-safe.
Constructors of StringBuffer Class
• StringBuffer( )
This constructor constructs a string buffer with no characters in it and
an initial capacity of 16 characters.
• StringBuffer(String str)
This constructor constructs a string buffer initialized to the contents of
the specified string with room for additional 16 characters.
• StringBuffer(int capacity)
This constructor constructs a string buffer with no characters in it and
the specified initial capacity.
• StringBuffer(CharSequence chs)
This constructor constructs a string buffer that contains the same
characters as the specified CharSequence.
Common Methods of StringBuffer Class
1. length()
This method returns the length of the StringBuffer .
2. capacity()
This method return the total capacity of StringBuffer i.e. length of
StringBuffer+ length of extra room for characters.
3. ensureCapacity(int capacity)
This method is used to increase or decrease the capacity of StringBuffer.
4. append(String str)
This method appends specified string at the end of string. Apart from
string , it can also append all primitive types, objects and CharSequence.
Every time this method is called for a particular parameter an implicit call to
valueOf() method is made to convert it into string representation.
Common Methods of StringBuffer Class
5. setCharAt(int index, char ch)
This method sets the value of a character at specified index.
charAt() It can be used to get the character at specified index as
discussed before.
6. insert()
This method is used to insert one string into another string at
specified index. Apart from string we can insert any primitive types,
Object type and CharSequence. Like append() method this method
also calls valueOf() method to obtain string representation of
parameter.
7. reverse()
This method reverse the characters in StringBuffer object.
Common Methods of StringBuffer Class
8. substring()
This method extract a portion of string(substring) specified from the StringBuffer
string.
Syntax : It accepts parameters i.e. a start index or it can also accept start index
and end index.
9. replace()
This method replace this StringBuffer string with specified string from
specified startindex to endIndex-1 .
Syntax : replace(int startIndex, int endIndex, String repStr)
10. delete()
This method deletes the characters of StringBuffer object from specified
startIndex to endIndex-1.
Syntax : delete(int startIndex, int endIndex);
11. deleteCharAt()
This method deletes a character of StringBuffer object from a specified index.
StringBuilder Class
• StringBuilder class under strings in java create mutable string objects.
This class is similar to StringBuffer class except that StringBuilder
class is not thread safe, hence no guarantee of synchronization. Also
StringBuilder is faster in performance.
• The methods of StringBuilder are same as that of StringBuffer , only
difference is that methods in StringBuffer are synchronized whereas
that of StringBuilder are not.
StringBuilder Constructors
• StringBuilder (): creates an empty StringBuilder and reserves room for
16 characters.
• StringBuilder (int size): create an empty string and takes an integer
argument to set capacity of the buffer.
• StringBuilder (String str): create a StringBuilder object and initialize it
with string str.
• StringBuilder (CharSequence seq): It creates stringbuilder object by
using CharSequence object.