Java StringBuilder append() Method

The StringBuilder.append() method in Java is used to append various types of data to the existing sequence of characters in a StringBuilder object. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.

Table of Contents

  1. Introduction
  2. append Method Syntax
  3. Examples
    • Appending Strings
    • Appending Characters
    • Appending Integer Values
    • Appending Boolean Values
    • Appending Double Values
    • Appending Char Arrays
    • Appending Substrings
    • Appending Objects
  4. Conclusion

Introduction

The StringBuilder.append() method is a member of the StringBuilder class in Java. It allows you to efficiently append different types of data to a StringBuilder object without creating a new object for each modification, making it a preferred choice for string manipulation in performance-critical applications.

append Method Syntax

The syntax for the append method is as follows:

public StringBuilder append(String str)
public StringBuilder append(char c)
public StringBuilder append(int i)
public StringBuilder append(boolean b)
public StringBuilder append(double d)
public StringBuilder append(char[] str)
public StringBuilder append(char[] str, int offset, int len)
public StringBuilder append(CharSequence s, int start, int end)
public StringBuilder append(Object obj)

Examples

Appending Strings

The append method can be used to concatenate strings to the existing sequence in a StringBuilder object.

Example

public class StringBuilderAppendExample {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Hello");

        sb.append(", World!");

        System.out.println(sb.toString());
    }
}

Output:

Hello, World!

Appending Characters

You can append a single character to the StringBuilder.

Example

public class StringBuilderAppendExample {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Hello");

        sb.append('!');

        System.out.println(sb.toString());
    }
}

Output:

Hello!

Appending Integer Values

You can append integer values directly to the StringBuilder.

Example

public class StringBuilderAppendExample {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Count: ");

        sb.append(10);

        System.out.println(sb.toString());
    }
}

Output:

Count: 10

Appending Boolean Values

You can append boolean values to the StringBuilder.

Example

public class StringBuilderAppendExample {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Active: ");

        sb.append(true);

        System.out.println(sb.toString());
    }
}

Output:

Active: true

Appending Double Values

You can append double values to the StringBuilder.

Example

public class StringBuilderAppendExample {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("PI: ");

        sb.append(3.14159);

        System.out.println(sb.toString());
    }
}

Output:

PI: 3.14159

Appending Char Arrays

You can append character arrays to the StringBuilder.

Example

public class StringBuilderAppendExample {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Array: ");
        char[] charArray = {'a', 'b', 'c'};

        sb.append(charArray);

        System.out.println(sb.toString());
    }
}

Output:

Array: abc

Appending Substrings

You can append a subsequence from a CharSequence to the StringBuilder.

Example

public class StringBuilderAppendExample {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Message: ");
        CharSequence seq = "Hello, World!";

        sb.append(seq, 7, 12);

        System.out.println(sb.toString());
    }
}

Output:

Message: World

Appending Objects

You can append the string representation of any object to the StringBuilder.

Example

public class StringBuilderAppendExample {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Object: ");
        Object obj = 123;

        sb.append(obj);

        System.out.println(sb.toString());
    }
}

Output:

Object: 123

Conclusion

The StringBuilder.append() method in Java is a versatile and efficient way to build and modify strings dynamically. By understanding how to use the various overloaded versions of this method, you can optimize your string manipulation operations, especially in scenarios where performance is crucial. Whether you need to append strings, characters, numeric values, or objects, the append method provides a powerful solution for these tasks.

Comments