Let's learn java programming language with easy steps

Sunday, 16 April 2017

Java StringBuilder

 StringBuilder

StringBuilder is a class in java.lang.package. Java StringBuilder class is mutable like StringBuffer class i.e it is used to create mutable or modifiable string in java. StringBuilder class is same as StringBuffer class except it is non-synchronized .

StringBuilder class is not thread-safe or non-synchronized i.e multiple thread can access it simultaneously. 


StringBuilder Class Constructors

(1) StringBuilder()

It creates an StringBuilder object with the initial capacity of 16.

(2) StringBuilder(String s)

It creates the StringBuilder object with the specified string.

(3) StringBuilder(int capacity)

It create an empty StringBuilder object with the specified capacity.


Methods of StringBuilder class in Java

  1. public StringBuilder append(String s).
  2. public StringBuilder insert(int offset, String s).
  3. public StringBuilder replace(int startIndex, int endIndex, String s).
  4. public StringBuilder delete(int startIndex, int endIndex).
  5. public StringBuilder reverse();
  6. public int capacity();
  7. public void ensureCapacity(int minimumCapacity).
  8. public char chatAt(int index)
  9. public int length();
  10. public String substring(int beginIndex).
  11. public String substring(int beginIndex, int endIndex).

Java StringBuilder Examples

In this StringBuilder examples, we will learn many methods of StringBuilder class in java. Let's start

1) StringBuilder append() method

This append() method is used to add the new string at the end of original string.

For example :

class Simple
{
public static void main(String args[])
{
StringBuilder sb = new StringBuilder("hello ");
sb.append("java");//original string will changed
System.out.println(sb);
}
}

output : hello java

2) StringBuilder insert() method

The insert() method of StringBuilder class is used to insert the new string with original string at specified position.

For example : 

class Simple1
{
public static void main(String args[])
{
StringBuilder sb = new StringBuilder("hello ");
sb.insert(2,"java");
System.out.println(sb);
}
}

output : hejavallo

3) StringBuilder replace() method

The replace() method of StringBuilder class is used to replace any old string with new string based on index value.

For example : 

class Simple2
{
public static void main(String args[])
{
StringBuilder sb = new StringBuilder("hello ");
sb.replace(1,3,"java");
System.out.println(sb);
}
}

output : hejavallo

4) StringBuilder delete() method

The delete() method of StringBuilder class deletes the string from the specified beginIndex to endIndex.

For example : 

class Simple3
{
public static void main(String args[])
{
StringBuilder sb = new StringBuilder("hello");
sb.delete(1,3);
System.out.println(sb);
}
}

output : hlo

5) StringBuilder reverse() method

The reverse() method of StringBuilder class is used to reverse the given string.

For example :

class Simple4
{
public static void main(String args[])
{
StringBuilder sb = new StringBuilder("hello");
sb.reverse();
System.out.println(sb);
}
}


output : olleh

6) StringBuilder capacity() method

The StringBuilder capacity() method returns the current capacity of the Builder. The default capacity of the Builder is 16. If the total number of characters increases from it current capacity , it increases the capacity by ( oldcapacity * 2) +2. For example, if your current capacity is 16, it will be (16*2)+2 = 34.

For example : 

class CapacityMethodExample
{
public static void main(String args[])
{
StringBuilder sb = new StringBuilder();
System.out.println(sb.capacity());//default capacity 16
sb.append("hello");
System.out.println(sb.capacity());//still capacity is 16
sb.append("java is most popular language");
System.out.println(sb.capacity());//now (16*2)+2 = 34
}
}

output : 16
               16
               34

7) StringBuilder ensureCapacity() method

The ensureCapacity() method ensures that the given capacity is minimum to the current capacity. If it is greater than the current capacity the it increase the capacity by (oldcapacity*2)+2. for example current capacity is 16.

For example : 

class EnsureCapacityMethodExample
{
public static void main(String args[])
{
StringBuilder sb = new StringBuilder();
System.out.println(sb.capacity());//default capacity 16
sb.append("hello");
System.out.println(sb.capacity());//still capacity is 16
sb.append("java is most popular language");
System.out.println(sb.capacity());//now (16*2)+2 = 34
sb.ensureCapacity(10);//now no change
System.out.println(sb.capacity());//now 34
sb.ensureCapacity(50);//(34*2)+2

System.out.println(sb.capacity());//70
}

}

output : 16
              16
              34
              34
              70


Difference between StringBuffer and StringBuilder

difference between StringBuffer and StringBuilder
Share:

0 comments:

Post a Comment

Facebook Page Likes

Follow javatutorial95 on Google+

Follow javatutorial95 on twitter

Popular Posts

Get Updates By Email

Link

Translate