Add character to string java: How to implement it in Java

Java is an in the language setting object. It is used in developing software, websites, games, or applications on mobile devices. So if you are a developer or programmer, you need to know about java. There are many things you need to know about Java. So, in this article, the ArrowHitech team will show you information about adding characters to a string java.

What is string in Java ?

Generally, a String is a sequence of characters. But in Java, a string is an object that represents a sequence of characters. The java String class is used to create a string object.

Related post: Java stop program: How to end the program in Java

Adding characters to a string java

There are multiple ways to add character to String.

1. Add to the start of String

You can add character at start of String using + operator.

char startChar=’J’;

String blogName =  “ava2blog”;

String cblogName =  startChar + blogName;

2. Add to the end of String

You can add character at start of String using + operator. For example:

char endChar =’g’;

String blogNameWOG =  “Java2blo”;

String blogNameWG =  blogNameWOG + endChar;

Here is the complete program to add character at start and end of the String. Let’s following:

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);    }}

As you can see, we have added ‘J’ to the start of String Java “ava2blog” and added ‘g’ to the end of “Java2blo”.

3. Add character to String at given postion

There are several methods for adding characters to a string java at the given position. Firstly, you can use StringBuffer's insert() method tfor adding characters to a string java at the given position. So, let’s see with the help of an example:

package org.arpit.java2blog;

public class AddCharacterToStringAnyPosition { 

public static void main(String[] args) {       

 String blogName = “JavaBlog”;       

 char two =’2′;        

StringcblogName=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();

Moreover, as you can see, we have added character ‘2’ at position 4 to String “Java blog”. Secondly, you can also use String’s substring method for adding characters to a string java at a given position. Then, let’s see with the help of an example.

example of the way to add character to string java

In addition to, I will explanation for you:

  • Get a substring before the positon
  • Then, add character
  • Finally, get a substring after the position.

Although this looks easy and readable solution, there is a disadvantage of using this approach. Then, we already know String is immutable in java. Each substring call creates a new String object. So, when we use+operator to concatenate string, it internally creates StringBuffer objects as well. Moreover, 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.

The third way, you can also use Apache common’s StringUtils to add character to the string Java at any postion. Next to the step, please follow the dependency which you need to add for Apache common lang3 in pom.xml.

apache common's stringUtils

However, if you want to understand better, follow the example below.

example of Apache common’s StringUtils

Finally, as you can see, we have used StringUtils‘s overlay() method for adding characters to a string java at a given position. Since we just want to add one character, start and end positions are the same pos

Conclusion

In general, we have covered all the information about adding characters to a string java. We hope this is a useful article for you. So if you want to learn more about Java as well as the steps to implement it. So please click HERE to contact us. At Arrowhitech, our professionals have expertise in building business-specific web applications and mobile app development services that meet your high expectations and quality standards. 

Tags

Share