How to trim white space in StringBuffer in Java?


The String class of the java.lang package represents a set of characters. All string literals in Java programs, such as "abc", are implemented as instances of this class. Strings objects are immutable, once you create a String object you cannot change their values, if you try to do so instead of changing the value a new object is created with the required value and the reference shifts to the newly created one leaving the previous object unused.

The StringBuffer (and StringBuilder) class is used when there is a necessity to make a lot of modifications to a String.

Unlike Strings, objects of type StringBuffer can be modified over and over again without leaving behind a lot of new unused objects. It is a thread-safe, mutable sequence of characters.

Example

 Live Demo

public class StringBufferExample {
   public static void main(String[] args) {
      StringBuffer buffer = new StringBuffer();
      buffer.append("Hello ");
      buffer.append("how ");
      buffer.append("are ");
      buffer.append("you");
      System.out.println("Contents of the string buffer: "+buffer);
   }
}

Output

Contents of the string buffer: Hello how are you

Trimming the white space

StringBuffer() does not provide any method to remove the white spaces between its contents.

The trim() method of the String class initially makes a copy of the current String, removes its leading and trailing spaces and returns it.

To remove leading and trailing spaces in a StringBuffer −

  • You need to convert the StringBuffer object into a String object using the toString() method.

  • Invoke the trim() method on the result.

Example

 Live Demo

public class StringBufferCapacity {
   public static void main(String[] args) {
      StringBuffer buffer = new StringBuffer();
      buffer.append(" Hello ");
      buffer.append("how ");
      buffer.append("are ");
      buffer.append("you ");
      System.out.println("Contents of the string buffer: "+buffer);
      //Converting StringBuffer to String
      String str = buffer.toString();
      //Removing the leading and trailing spaces
      System.out.println(str.trim());
   }
}

Output

Contents of the string buffer: Hello how are you

If you want to remove spaces from the StringBuffer completely, one way to do so is −

  • Remove the leading and trailing zeroes from it.

  • Convert the StringBuffer object to a String value using the toString() method.

  • The split() method of the String class accepts a delimiter (in String format), splits the given string to an array of Strings (based on the given delimiter).

    Split the String obtained in the previous step using this method.

  • Append each element in the array obtained to another StringBuffer.

Example

 Live Demo

public class StringBufferCapacity {
   public static void main(String[] args) {
      StringBuffer buffer = new StringBuffer();
      buffer.append(" Hello ");
      buffer.append("how ");
      buffer.append("are ");
      buffer.append("you ");
      System.out.println("Contents of the string buffer: "+buffer);
      //Converting StringBuffer to String
      String str = buffer.toString();
      //Removing the leading and trailing spaces
      str = str.trim();
      //Splitting the String
      String array[] = str.split(" ");
      //Appending each value to a buffer
      StringBuffer result = new StringBuffer();
      for(int i=0; i<array.length; i++) {
         result.append(array[i]);
      }
      System.out.println("Result: "+result);
   }
}

Output

Contents of the string buffer: Hello how are you
Result: Hellohowareyou
raja
Published on 10-Sep-2019 16:58:48
Advertisements