Add char to string
In this article, we will go through different ways to add character to string in java. This article will cover following character additions:
1. Add Character at the beginning or start of string.
2. Add Character at the end of string.
3. Insert character at any position in between string.

1. Using plus(+) operator
A character can be added to a string using plus(+) operator. When a character is added to a string, the result is also a string.
Below is an example program which adds a character at the beginning of the string

public class AddCharacterExample {
  public static void main(String[] a) {
    String s = "owl";
    char c = 'B';
    s = c + s'
    System.out.println("String with character added to beginning: " + s);
  }
}

This code prints

String with character added to beginning: Bowl

Add character after string
A character can be appended to a string using plus(+) operator between the string and the character as shown below.

public class AddCharacterExample { 
  public static void main(String[] a) { 
    String s = "Hell"; 
    char c = 'o'; 
    s = s + c;
    System.out.println("String with character added at the end: " + s); 
  } 
}

Output is

String with character added at the end: Hello

2. Using String.getChars()
java.lang.String class has a getChars() method which copies the characters from a string between given range into a new character array.
It takes the following arguments
1. Start index of string from which characters will be copied.
2. End index of string.
3. character array in which the characters will be copied.
4. Start index of destination array where characters will be copied.

So, to insert a ‘-‘ between “HelloWorld”,
A. use getChars() method with start index as 0, end index as the position at which the new character needs to be inserted. In this case, it will be 5(indexing starts from 0).
This will copy the characters from the beginning of the string till that position into the destination array.

B. Insert the new character at required position in the destination array. It will be 6, in this example.

C. Use getChars() again with start index as the position of next character in the string, and end index as the position of last character of the string.
For this example, start index will be 6, end index will be the length of the string(index of last character).

D. Convert character array to a string using String class constructor.

Java program based on this logic is given below

public class AddCharacterExample {
  public static void main(String[] a) {
    String s = "HelloWorld";
    int length = s.length();
    // define array 1 more than the string size
    char[] c = new char[length + 1];
    // get characters from start till insert position
    s.getChars(0, 5, c, 0);
    // insert character ahead
    c[6] = '-';
    // insert remaining chars
    s.getChars(6, length, c, 7);
    String st = new String(c);
    System.out.println("Actual string is: " + s); 
    System.out.println("Modified string is: " + st);  
  }
}

Output is

Actual string is: HelloWorld
Modified string is: Hello-World

To add a character before the string with this method, use the following code

String st = "owl";
int length = st.length();
char[] c = new char[length + 1];
// add first character
c[0] = 'B';
// copy remaining characters
st.getChars(0, st.length(), c, 1);
String s = new String(c);

Similarly, you can add a character at the end of string.

3. Using StringBuffer
java.lang.StringBuffer has an insert() method which can be used to add a character to a string. It takes following 2 arguments
1. Position at which the character should be inserted.
2. Character that needs to be inserted.

Example program is

StringBuffer s = new StringBuffer("HelloWorld");
s.insert(5, '-');
// will print Hello-World
System.out.println(s.toString());

Similarly, you can use java.lang.StringBuilder class, which also has an insert() method.
This method is concise and more readable. Since StringBuffer is mutable, it does not create any extra objects.
4. Using substring()
String class has a substring() method which takes two integer arguments and returns the characters between those indexes as a string.
To insert a character between a string, use substring() twice to break the string between two parts and add the desired character between those two parts to create the final string as shown below.

String st = "HelloWorld";
// s1 will be "Hello"
String s1 = st.substring(0, 5);
// s1 will be "World"
String s2 = st.substring(5);
// add character in between
String result = s1 + '-' + s2;

This method is similar to getChars() discussed above.

This method has the disadvantage that it will create 3 different string objects for a single insertion, since string is an immutable class and every operation creates a new string object.
So, two substring() operations will create two string objects and one addition will create another object.

5. Using Apache Commons Library
StringUtils class from Apache Commons library has a overlay() method which is used to replace or overlay a part of string with another string.
Its takes 4 methods as arguments:
1. The string.
2. Overlay string or the characters that will be inserted.
3. start index from which overlay will begin.
4. end index at which overlay will end.

So, if the overlay string is a single character that you want to insert, start and end index are the same, then the character will be inserted in between. Example,

String s = "HelloWorld";
String overlay = StringUtils.overlay(s, "-", 5, 5);
// will print Hello-World
System.out.println(s);

Add following dependency to include Apache Commons library to your project

Maven

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.12.0</version>
</dependency>

Gradle

implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.12.0'

Click the clap if the article was useful.