Java

How to Add char to String in Java

char” is a primitive data type belonging to the Java wrapper class “Character” that stores 16-bit Unicode characters. Java uses the char data type to store one character that is enclosed in single quotes (‘ ‘), While a “String” is used to hold a group of characters that are arranged in a particular order surrounded by double quotes (” “). Java provides multiple options for adding a new character to a String.

This blog will discuss the procedure of adding char to a String in Java.

How to Add Char to String in Java?

For adding a character “char” to a String, you can use:

  • Concatenation Operator “+
  • substring() method
  • append() method
  • insert() method

Let’s have a look at these methods one by one!

Method 1: Add char to String Using Concatenation Operator “+”

The easiest and most commonly used method to add char to a String is the Concatenation operator “+”. It concatenates the character to a String. You can add additional characters either at the start, middle, or at the end of the String with the help of the “+” operator.

Syntax
The following syntax can be utilized to add char “ch” to a String “str”:

ch + str

Example 1: Add char at the Start of the String
In this example, we will add the character “H” at the start of a String. To do so, we will create a char named “ch” with the specified values:

char ch = 'H';

Next, we will concatenate the created char with the substring “ello” and store the resultant value in the “s” String:

String s = ch + "ello";

Finally, we will print the value of our String:

System.out.println(s);

The output shows that the character “H” is now concatenated with the “ello” substring, and the resultant String is “Hello”:

Example 2: Add char at the Middle of the String
Here, we will add a character “n” in the middle of the String:

char ch = 'n';

Now, we will add the value of the created characters in between the “Li” and “ux” substrings, with the help of the “+” operator:

String s = "Li" + ch + "ux";

Then, simply print the value of “s” using the “System.out.println()” method:

System.out.println(s);

As you can see, we have successfully added the specified char in the middle:

Example 3: Add char at the End of the String
Now, we will check how to add a character at the end of the String. We will have a character type variable “ch” that stores a character “t”:

char ch = 't';

Here, we have a String “Linux Hin”, which will be concatenated with the “ch” character and store the resultant value in “s” String:

String s = "Linux Hin" + ch;

At last, print the value of String type variable “s” on the console:

System.out.println(s);

Output

Let’s check other methods for adding char to the String in Java.

Method 2: Add char to String Using substring() Method

In Java, another method for adding a character to a String is the “substring()” method. It belongs to the String class.

Syntax
Here is the syntax of the “substring()” method for the specified purpose:

s.substring(firstIndex,secondIndex) + ch + s.substring(secondIndex)

Call the substring() method with a String “s” and split it by passing the start index as a “firstIndex” and the index where you want to add the character as “secondIndex”. Now, add the character using the “+” operator and then concatenate the other part of the String starting from the passed “secondIndex”.

Example
We will add character “n” at the middle of the String using the “substring()” method:

char ch = 'n';

The String “LiuxHint” is stored in the String type variable “s”. We want to add the character “n” before the character “u” that is placed at the “2” index:

String s = "LiuxHint";

We will split the String from the start to the 2nd index of the String “s” using “s.substring(0,2)” and add the character “ch” at that place, and then concatenate the remaining part of the String with “s.substring(2)” the start index of that and save it in a String type variable “sb”:

String sb= s.substring(0,2) + ch + s.substring(2);

Finally, print the value of “sb” on the console window:

System.out.println(sb);

The output signifies that the “n” character is added to the “LiuxHint” String, and it becomes “LinuxHint”:

There are some other methods for adding characters to a String. Let’s move towards them.

Method 3: Add char to String Using append() Method

The “append()” method of the “StringBuilder” class is also used to add a character to a String in Java. In the StringBuilder class, you can build a String with concatenation. It works the same as the “+” operator.

Syntax
Follow the below syntax for using the “append()” method:

sb.append(ch)

The append() method will be called with the object of the StringBuilder class “sb”, and it takes a character “ch” as an argument.

Example

In this example, we will add the character “t” at the end of the String “LinuxHin” by utilizing the “append()” method:

char ch = 't';

The String “LinuxHin” will store in a String type variable “s”:

String s = "LinuxHin";

We will create an object “sb” of the “StringBuilder” class:

StringBuilder sb = new StringBuilder();

Now, we will call the “append()” method with object “sb” by passing it in a String “s” and then again call “append()” method and pass character “ch” as an argument:

sb.append(s).append(ch);

Finally, we will print the object “sb” that contains the resultant String by adding a character in it:

System.out.println(sb);

The output shows that we have successfully added the character “t” at the end of the “LinuxHin” substring:

There is one more method for adding characters in between the Strings. Let’s check it out.

Method 4: Add char to String Using insert() Method

The “insert()” method of the “StringBuffer” class is also used to add characters in a String in Java. It adds the character at the specified position, similar to the substring() method. The characters and substrings can also be placed in the middle or appended at the end of a StringBuffer.

Syntax
The “insert()” method has the following syntax:

sb.insert(index, ch);

The insert() method is called with the object of the StringBuffer class “sb” by passing an “index” where you want to add the “ch” character.

Example
In this example, we will add the character “u” at the 3rd index of the String “LinxHint”. For this purpose, we have created a “ch” character:

char ch = 'u';

The String “LinxHint” is stored in the “s” variable:

String s = "LinxHint";

Then, we will create an object “sb” of the StringBuffer class and pass the created String as an argument to it:

StringBuffer sb = new StringBuffer(s);

Call the “insert()” method and pass character “ch” and the index as “3”:

sb.insert(3, ch);

Lastly, we will print the value of object “sb” with the help of the “System.out.println()” method:

System.out.println(sb);

The output shows that the character “u” is successfully added in the String “LinuxHint” at the 3rd index:

We have compiled all the methods related to adding a character to a String in Java.

Conclusion

For adding characters to a String, you can use the Concatenation Operator “+”, substring(), append(), and insert() method. The most common and easiest way to add character to a String. You can also add character at any place at either the String’s start, end, or middle by utilizing the “+” operator and the append() method. While for other methods, you have to mention the index. In this blog, we have discussed the methods for adding a character to a String in Java with detailed examples.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.