I just announced the new Spring Boot 2 material, coming in REST With Spring:

>> CHECK OUT THE COURSE

1. Introduction

In this quick tutorial, we’ll demonstrate how to add a character at any given position in a String in Java.

We’ll present three implementations of a simple function which takes the original String, a character and the position where we need to add it.

Since the String class is final and immutable the function should return a new String with the added character.

2. Using a Character Array

Here, the idea is to create a new character array and copy the characters from the original String before the given position.

After that, we put the new character at the position and copy the rest of the characters from the original String in the subsequent positions of the new array.

Finally, we construct the desired String from that array.

public String addChar(String str, char ch, int position) {
    int len = str.length();
    char[] updatedArr = new char[len + 1];
    str.getChars(0, position, updatedArr, 0);
    updatedArr[position] = ch;
    str.getChars(position, len, updatedArr, position + 1);
    return new String(updatedArr);
}

Compared to the other two methods, this is a low-level design approach and gives us the most flexibility.

3. Using the substring Method

An easier and higher level approach is to use the substring() method of the String class. It prepares the String by concatenating:

  1. The substring of the original String before the position
  2. The new character
  3. The substring of the original String after the position
public String addChar(String str, char ch, int position) {
    return str.substring(0, position) + ch + str.substring(position);
}

Although the above code is more readable, it has a downside in that it creates a number of temporary objects to determine the result. As String is an immutable class, every call to its substring() method creates a new String instance.

Finally, when we concatenate the parts, the compiler creates a StringBuilder object for appending them one by one. Every String and StringBuilder object allocates separate memory locations for its internal character array.

This implementation also needs to copy all the characters thrice from one array to another.

If we need to call the method a huge number of times, the temporary objects may fill the heap memory and that will trigger GC very frequently. This can also affect the performance to some extent.

4. Using a StringBuilder

StringBuilder is a utility class provided by Java library to construct and manipulate String objects in a number of ways.

We can implement the same functionality using the insert() method of the StringBuilder class:

public String addChar(String str, char ch, int position) {
    StringBuilder sb = new StringBuilder(str);
    sb.insert(position, ch);
    return sb.toString();
}

The above code needs to create only a single StringBuilder object to insert the character at the position. It allocates the same amount of memory that the original String has, but to create a place for the new character the underlying array shifts the next characters by 1 position.

Although using a StringBuilder may be slower, it doesn’t have the memory burden of initializing temporary objects. We also end up with code that is simple and readable.

5. Conclusion

In this article, we focused on several ways of adding a character in a String object in Java. We’ve seen that the implementation using a character array offers the best performance and that with the substring method gives a more readable approach.

The preferred way of implementing the solution is using the StringBuilder class – as it’s simple, less bug-prone and offers good and stable performance.

As usual, the complete source code for the above tutorial is available over on GitHub.

I just announced the new Spring Boot 2 material, coming in REST With Spring:

>> CHECK OUT THE LESSONS