org.apache.commons.lang3.text.StrBuilder Example

Q

What is org.apache.commons.lang3.text.StrBuilder class? How to use org.apache.commons.lang3.text.StrBuilder class?

✍: FYIcenter.com

A

org.apache.commons.lang3.text.StrBuilder class is a Java class offered in commons-lang3.jar that builds a string from constituent parts providing a more flexible and powerful API than StringBuffer

Methods in StrBuilder class that are note StringBuffer and StringBuilder classes:

  • appendWithSeparators - adds an array of values, with a separator
  • appendPadding - adds a length padding characters
  • appendFixedLength - adds a fixed width field to the builder
  • toCharArray/getChars - simpler ways to get a range of the character array
  • delete - delete char or string
  • replace - search and replace for a char or string
  • leftString/rightString/midString - substring without exceptions
  • contains - whether the builder contains a char or string
  • size/clear/isEmpty - collections style API methods

Here is a simple example of org.apache.commons.lang3.text.StrBuilder class:

// Copyright (c) 2016 FYIcenter.com
import org.apache.commons.lang3.text.StrBuilder;

// Example of using the StrBuilder class
public class StrBuilderExample {
   public static void main(String[] args) throws Exception {
      
      // Create an empty StrBuilder
      StrBuilder builder = new StrBuilder();
      
      // Append a String
      builder.append("Emergency ");

      // Append a StrBuilder
      builder.append(new StrBuilder("number:"));

      // Append a newline
      builder.appendNewLine();
      
      // Append a char[]
      builder.append(new char[6]);

      // Append an int
      builder.appendln(911);

      // Build the final String
      String str = builder.build();

      System.out.println(str);
   }
}

You can compile and run the above example in a command window as shown below:

C:\fyicenter>\local\jdk-1.8.0\bin\javac 
   -cp C:\local\commons-lang3-3.5\commons-lang3-3.5.jar
   StrBuilderExample.java

C:\fyicenter>\local\jdk-1.8.0\bin\java
   -cp .;C:\local\commons-lang3-3.5\commons-lang3-3.5.jar
   StrBuilderExample

Emergency number:
      911

 

org.apache.commons.lang3.text.StrSubstitutor Example

Class Packages in Apache commons-lang3-3.5.jar

Using commons-lang3.jar in Java Programs

⇑⇑ FAQ for Apache commons-lang.jar

2016-11-24, 1977🔥, 0💬