In this post, we will see how to add character to String in java.
There are multiple ways to add character to String.
Table of Contents
Add character to the start of String
You can add character at start of String using +
operator.
1 2 3 4 5 |
char startChar='J'; String blogName = "ava2blog"; String cblogName = startChar + blogName; |
Add character to the end of String
You can add character at start of String using +
operator.
1 2 3 4 5 |
char endChar ='g'; String blogNameWOG = "Java2blo"; String blogNameWG = blogNameWOG + endChar; |
Further reading
How to compare characters in Java
Java remove last character from string
How to check if String has all unique characters in java
Here is the complete program to add character at start and end of the String.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package org.arpit.java2blog; public class AddCharacterStartEndMain { public static void main(String[] args) { char startChar='J'; String blogName = "ava2blog"; String cblogName = startChar + blogName; System.out.println(cblogName); char endChar ='g'; String blogNameWOG = "Java2blo"; String blogNameWG = blogNameWOG + endChar; System.out.println(blogNameWG); } } |
Output:
Java2blog
As you can see, we have added 'J'
to start of String "ava2blog"
and added 'g'
to the end of "Java2blo"
.
Add character to String at given postion
There are several methods to add Add character to String at given position.
Using StringBuffer
You can use StringBuffer's
insert()
method to add character to String at given position.
Let’s see with the help of an example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package org.arpit.java2blog; public class AddCharacterToStringAnyPosition { public static void main(String[] args) { String blogName = "JavaBlog"; char two ='2'; String cblogName = addCharToString(blogName,two,4); System.out.println(cblogName); } public static String addCharToString(String str, char c, int pos) { StringBuilder stringBuilder = new StringBuilder(str); stringBuilder.insert(pos, c); return stringBuilder.toString(); } } |
Output:
As you can see, we have add char '2'
at position 4
to String "Javablog"
.
In case, you want thread safe code, then you should use StringBuffer instead of StringBuilder.
Using substring
You can also use String’s substring method to add character to String at given position.
Let’s see with the help of an example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package org.arpit.java2blog; public class AddCharacterToStringAnyPosition { public static void main(String[] args) { String blogName = "JavaBlog"; char two ='2'; String cblogName = addCharToStringUsingSubString(blogName,two,4); System.out.println(cblogName); } public static String addCharToStringUsingSubString(String str, char c, int pos) { return str.substring(0, pos)+ c +str.substring(pos); } } |
Output:
Explanation
- Get a substring before the positon
- Add character
- Get a substring after the positon
Although this looks easy and readable solution, there is disadvantage of using this approach.
As we already know String is immutable in java. Each substring call creates new String object.
When we use +
operator to concatenate string, it internally creates StringBuffer objects as well.
If we need to call this method many times, then this may cause frequent garbage collection as heap memory will be filled faster due to temporary objects.
Using Apache common lang3
You can also use Apache common’s StringUtils
to add character to the string at any postion
Here is the dependency which you need to add for Apache common lang3 in pom.xml
.
1 2 3 4 5 6 7 8 9 |
</p> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.9</version> </dependency> <p> |
Here is the example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package org.arpit.java2blog; package org.arpit.java2blog; import org.apache.commons.lang3.StringUtils; public class AddCharacterToStringAnyPosition { public static void main(String[] args) { String blogName = "JavaBlog"; char two ='2'; String cblogName = addCharToStringUsingApache(blogName,two,4); System.out.println(cblogName); } public static String addCharToStringUsingApache(String str, char c, int pos) { return StringUtils.overlay(str,""+c, pos, pos); } } |
Output:
As you can see, we have used StringUtils
‘s overlay()
method to add character to the String at given position.
StringUtils.overlay(str, overlay, start, end)
takes four arguments.
str
: Orignal String
overlay
: String which you want to add
start
: start position
end
: end position
Since we just want to add one character, start and end positions are the same pos
.
Conclusion
We have seen multiple ways to add character to String at various positions.
That’s all about Add character to String in java.