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”:
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:
Next, we will concatenate the created char with the substring “ello” and store the resultant value in the “s” String:
Finally, we will print the value of our String:
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:
Now, we will add the value of the created characters in between the “Li” and “ux” substrings, with the help of the “+” operator:
Then, simply print the value of “s” using the “System.out.println()” method:
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”:
Here, we have a String “Linux Hin”, which will be concatenated with the “ch” character and store the resultant value in “s” String:
At last, print the value of String type variable “s” on the console:
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:
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:
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:
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”:
Finally, print the value of “sb” on the console window:
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:
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:
The String “LinuxHin” will store in a String type variable “s”:
We will create an object “sb” of the “StringBuilder” class:
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:
Finally, we will print the object “sb” that contains the resultant String by adding a character in it:
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:
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:
The String “LinxHint” is stored in the “s” variable:
Then, we will create an object “sb” of the StringBuffer class and pass the created String as an argument to it:
Call the “insert()” method and pass character “ch” and the index as “3”:
Lastly, we will print the value of object “sb” with the help of the “System.out.println()” method:
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.