How to add a character to the start and end of a string in Java

How to add a character to the start and end of a string in Java:

Let’s learn how to add characters to the start and end of a string in Java. We have different ways to add a character to the start or end of a string. In this post, I will show you how to do that in different ways.

String is immutable in Java. i.e. we can’t modify a string or its characters. If we are adding a character to the start or end of a string, it will create one new string.

Method 1: By using +:

We can add a character to the end or start of a string by using +. Note that it will create one new string.

Add a character to the start:

The below program adds a character to the start of a string by using +:

public class Main {

    public static void main(String[] args) {
        char c = '!';
        String givenStr = "Hello World";

        String newString = c + givenStr;

        System.out.println(newString);
    }
}

This program adds character c to the start of the string givenStr. It creates another string and returns that. This new string is stored in newString.

If you run this example, it will print the below output:

!Hello World

Add a character to the end:

Let’s add a character to the end of another string. We can use + to do that.

public class Main {

    public static void main(String[] args) {
        char c = '!';
        String givenStr = "Hello World";

        String newString = givenStr + c;

        System.out.println(newString);
    }
}

It will add the character to the end of givenStr. If you run this program, it will print:

Hello World!

Method 2: By using StringBuilder:

StringBuilder class has many advantages over string in Java. We can add characters to the same StringBuilder object and it will not create any new object for that. We can also convert a string to StringBuilder and vice versa.

StringBuilder has two methods for adding character to a string. One is called insert and another is called append. By using insert, we can add a character at any index of a StringBuilder and by using append, we can insert a character at the end.

Append a character to the end:

Let’s use append to append a character to the end of a string:

public class Main {

    public static void main(String[] args) {
        char c = '!';
        String givenStr = "Hello World";

        StringBuilder sb = new StringBuilder(givenStr);
        String newString = sb.append(c).toString();

        System.out.println("New string: "+newString);
    }
}

In this program, we have created one StringBuilder object and append the character by using the append method and convert it back to a string.

If you run this program, it will print Hello World!.

Append a character to the start:

Let’s use insert to insert a character to the start index of a string:

public class Main {

    public static void main(String[] args) {
        char c = '!';
        String givenStr = "Hello World";

        StringBuilder sb = new StringBuilder(givenStr);
        String newString = sb.insert(0, c).toString();

        System.out.println("New string: "+newString);
    }
}

It will print New string: !Hello World.

insert takes two parameters. The first one is the offset and second one is the character. We are providing 0 as the offset to add the character at the start.

You might also like: