Add Char to String in Java

Rupam Yadav Jan 30, 2023 Dec 13, 2020
  1. Java Add Char to String Using + Operator
  2. Java Add Char to String Using StringBuilder.append()
  3. Java Add Char to a String Using the substring() Method
Add Char to String in Java

This article will introduce how we can add a character to a string in Java. A character in Java is represented by the data type char, and it holds only a single value. We will use several methods to add char to string Java at different positions.

Java Add Char to String Using + Operator

This is the easiest and most straightforward way to add a character to a string in Java. We concatenate a char to the string using the + operator. In the program below, we have two char values - charToAdd1 and charToAdd2 which we will concatenate with strings - alex and bob.

In the first variable - alex, we have added charToAdd1 at the last position, while charToAdd2 is added in the middle. One thing to notice is that when we use the + concatenation, any data type like char will be converted to a string.

public class AddCharToString {
    public static void main(String[] args) {
        char charToAdd1 = 'A';
        char charToAdd2 = 'C';

        String alex = "Alex got Grade " + charToAdd1;
        String bob = "While Bob got " + charToAdd2 + " Grade";
      
        System.out.println(alex);
        System.out.println(bob);

    }
}

Output:

Alex got Grade A
While Bob got C Grade

Java Add Char to String Using StringBuilder.append()

In this method, we add char to string using the append() function of the StringBuilder class in Java. This function appends two or more strings just like the + operator.

In the below example, we create two StringBuilder objects and then first append the charToAdd1 to alex and then join charToAdd2 to bob.

public class AddChartToString {
    public static void main(String[] args) {

        char charToAdd1 = 'A';
        char charToAdd2 = 'C';

        StringBuilder stringBuilder1 = new StringBuilder();
        StringBuilder stringBuilder2 = new StringBuilder();

        String alex = "Alex got Grade ";
        String bob = "While Bob got Grade ";

        stringBuilder1.append(alex).append(charToAdd1);
        stringBuilder2.append(bob).append(charToAdd2);

        System.out.println(stringBuilder1);
        System.out.println(stringBuilder2);

    }
}

Output:

Alex got Grade A
While Bob got Grade C

Java Add Char to a String Using the substring() Method

This example uses the substring() method of the String class, which takes out a specified part of the string. In the code below, we can see that we first get the starting part of the alex by setting the position of the characters in the string. alex.substring(0, 15) takes the starting and the ending index.

Next, we will concatenate charToAdd1 using +, and at the end, we will join the remaining part of alex by alex.substring(15), where we pass the starting index as an argument.

We are doing the same for bobResult as it has a typo, and we want to fix it by adding the character ( g ). We will use the same solution for this.

public class AddChartToString {
    public static void main(String[] args) {

        char charToAdd1 = 'A';
        char charToAdd2 = 'g';

        String alex = "Alex got Grade  in the School";
        String bob = "While Bob ot Grade C";

        String alexResult = alex.substring(0, 15) + charToAdd1 +alex.substring(15);
        String bobResult = bob.substring(0, 10) + charToAdd2 + bob.substring(10);
        System.out.println(alexResult);
        System.out.println(bobResult);

    }
}

Output:

Alex got Grade A in the School
While Bob got Grade C
Author: Rupam Yadav
Rupam Yadav avatar Rupam Yadav avatar

Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.

LinkedIn

Related Article - Java Char

Related Article - Java String