Java Tutorial

String Handling

String Handling

A string is the sequence of characters enclosed within double quotes.

For example: "Java is a programming language"

In order to deal with strings we have two classes. They are java.lang.String and java.lang.StringBuffer

What is the difference between String and StringBuffer classes?

Answer: String class is by default not mutable (non-modifiable) and StringBuffer class is mutable (modifiable).

String API:

Constructors:

String (); - 1
String (String); - 2
String (char []); - 3

Constructor-1 is used for creating empty string. Constructor-2 is used for creating a String object by taking another string parameter. Constructor-3 is used for converting sequence of characters into string.

Instance methods:

public char charAt (int); - 1 
public int length (); - 2
public boolean equals (String); - 3
public boolean equalsIgnoreCase (String); - 4 
public String concat (String); - 5
public boolean startsWith (String); - 6 
public boolean endsWith (String); - 7

Method-1 is used for obtaining a character from a string by specifying valid character position. Method-2 is used for obtaining number of characters in the string. Method-3 is used for comparing two strings. If two strings are equal in its case and meaning then this method returns true otherwise false. Method-4 is used for comparing two strings by considering meaning by ignoring case. Method-5 is used for concatenating two strings and the result is stored in another string. Method-6 returns true provided the target String object present in first position of source String object otherwise false. Method-7 returns true provided the String object present in last position of source String object otherwise false.

Static methods:

public static String valueOf (byte); 
public static String valueOf (short); 
public static String valueOf (int); 
public static String valueOf (long); 
public static String valueOf (float); 
public static String valueOf (double); 
public static String valueOf (char); 
public static String valueOf (boolean);

These methods are used for converting any fundamental value into String object and this method is overloaded method.

public String substring (int start);- 1
public String substring (int start, int end); - 2

Method-1 is used for obtaining the characters from specified position to end character position. Method-2 is used for obtaining those characters by specifying starting position to ending position.

What is the similarity between String and StringBuffer classes?

Answer: Both String and StringBuffer classes are public final. Hence, they are not extended by any of the derived classes and we cannot override the methods of String and StringBuffer class.

StringBuffer class:

Whenever we create an object of StringBuffer we get 16 additional characters memory space. Hence an object of StringBuffer is mutable object.

StringBuffer API:

1.	StringBuffer ()
2.	StringBuffer (String)
3.	StringBuffer (char [])
4.	StringBuffer (int size)

Constructor-1 is used for creating an object of StringBuffer whose default capacity is 16 additional characters memory space.

For example:

StringBuffer sb=new StringBuffer (); 
System.out.println (sb.capacity ());

Constructor-2 is used for converting String object into StringBuffer object.

For example:

String s="HELLO";
StringBuffer sb=new StringBuffer (s); 
System.out.println (sb.capacity ()); 
System.out.println (sb.length ());

Constructor-3 is used for converting array of characters into StringBuffer object.

For example:

char ch [] = {'J', 'A', 'V', 'A'};
StringBuffer sb=new StringBuffer (ch);
System.out.println (sb);

Constructor-4 is used for creating an StringBuffer object with from specific size.

For example:

StringBuffer sb=new StringBuffer (256); 
Sb="JAVA IS AN APPLICATION";

Instance methods:

1. public int length (); This method is used for determining the length of the string.

2. public int capacity (); This method is used for determining the capacity of StringBuffer object. Capacity of StringBuffer object is equal to the number of characters in the StringBuffer plus 16 additional characters memory space.

For example:

StringBuffer sb=new StringBuffer ("HELLO"); 
System.out.println (sb.length ());
int cap=sb.capacity ();
System.out.println (cap);

3. public StringBuffer reverse (); This method is used for obtaining reverse of source StringBuffer object.

For example:

StringBuffer sb=new StringBuffer ("HELLO"); 
StringBuffer sb1=sb.reverse (); 
System.out.println (sb1);

4. public void append (byte);

5. public void append (short);

6. public void append (int);

7. public void append (long);

8. public void append (float);

9. public void append (double);

10. public void append (char);

11. public void append (boolean);

12. public void append (String);

All the above methods mean (4 to 12) are used for appending the numerical data or the string data at the end of source StringBuffer object.

13. public StringBuffer deleteCharAt (int); This method is used for removing the character at the specified position and obtaining the result as StringBuffer object.

For example:

StringBuffer sb=new StringBuffer ("JAVA PROGRAM"); 
StringBuffer sb1=sb.deleteCharAt (8); 
System.out.println (sb1);

14. public StringBuffer delete (int start, int end); This method is used for removing the specified number of characters from one position to another position.

For example:

StringBuffer sb=new StringBuffer ("JAVA PROGRAM"); 
StringBuffer sb1=sb.delete (5,8);
System.out.println (sb1);

15. public StringBuffer replace (String, int, int); This method is used for replacing the string into StringBuffer object form one specified position to another specified position.

For example:

StringBuffer sb=new StringBuffer ("JAVA PROGRAM"); 
System.out.println (sb1);

16. public StringBuffer insert (String, int, int); This method is used for inserting the string data from one specified position to another specified position.

For example:

StringBuffer sb=new StringBuffer ("JAVA PROGRAM"); 
StringBuffer sb1=sb.insert ("J2SE/, 0 ,3");
System.out.println (sb1);