Simply ways to add character to string java

A String object is a string of characters used to store data in an ordered sequence, an unlimited number of characters can be stored, a string can be represented on a single line of characters enclosed in quotes “”, like “hello” a string has 5 characters. Here are some ways to add characters to String Java.

Add a character to the beginning of the string.

add character to string java

Adding a character to the beginning of a string using the + operator.

You can see that we’ve added 'J' to start of String "ava2blog" and added 'g' to the end of "Java2blo".

Add a character to a string in the specified position

To add a character to a string at a certain point, there are numerous approaches.

Using StringBuffer

You can use method to add character to String at given position. To add a character to a String at a specified point, use the insert() function of the  StringBuffer's class.
Let’s examine with an illustration.

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:

Java2blog

As you can see, we have add char '2' at position 4 to String "Javablog".

Using substring

Another option is to add a character to a string at a certain point using the substring function of a String.
Using an illustration, let’s examine.

Output:

Java2blog

Explanation

  • Obtain a substring before to the position.
  • Add personality.
  • Obtain a substring following the position.

Although it appears to be a straightforward and understandable answer, there are drawbacks to this method.

As we already know, Java’s immutability of Strings. A new String object is created with each substring call.

StringBuffer objects are also created internally when we concatenate strings using the + operator.

As heap memory will fill up more quickly as a result of temporary objects, if we need to use this function often, this may result in frequent trash collection.

Using Apache common lang3

Additionally, you may add characters to the string at any position by using Apache Common’s StringUtils .
Here is the dependency that you must add to your  pom.xml file for Apache Common Lang 3.

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:

Java2blog

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

Because we are just adding one letter, the start and finish positions are both pos.

Conclusion

That’s all about Add character to string java, we hope you will have great experience with them. If there are barriers that are making it difficult for you, you can contact Onextdigital, which is always ready to help you.

Additionally, go no farther than Onextdigital if you’re seeking for a development for your eCommerce shop that is affordable. Along with a team of skilled experts and developers who work directly with the designers, we also provide economical Shopify Development, Bigcommerce Development, and Magento Development services.