How to add a char to a String in Java

In this tutorial, we will learn how to add a char to a String in Java. We can add a char at the
a. beginning
b. end, or,
c. at any given position of a String. Let's dive deep into the topic:
 
Read Also:  How to Compare Characters in Java

1. Using String

a. Beginning

Insert a character at the beginning of the String using the + operator.

public class JavaHungry {
    public static void main(String args[]) {
      char ch = 'J';
      String str = "avaHungry";
      //Adding character at the beginning
      str = ch + str; 
      System.out.println(str); 
    }
}

Output:
JavaHungry

b. End

Insert a character at the end of the String using the + operator.


public class JavaHungry {
    public static void main(String args[]) {
      char ch = 'y';
      String str = "JavaHungr";
      //Adding character at the end
      str =  str + ch; 
      System.out.println(str); 
    }
}

Output:
JavaHungry

2. Using StringBuilder

a. Beginning

Insert a character at the beginning of the String using StringBuilder's insert() method.

public class JavaHungry {
    public static void main(String args[]) {
      StringBuilder str = new StringBuilder("JavaHungry");
      /*Adding character at the beginning
      using StringBuilder insert() function */
      str.insert(0,'A');
      System.out.println(str); 
    }
}

Output:
AJavaHungry

b. End

Insert a character at the end of the String using StringBuilder's append() method.

public class JavaHungry {
    public static void main(String args[]) {
      StringBuilder str = new StringBuilder("JavaHungry");
      /*Adding character at the end
      using StringBuilder append() function */
      str.append('A');
      System.out.println(str); 
    }
}

Output:
JavaHungryA

c. At any position

Insert a character at any position of the String using StringBuilder's insert() method.

public class JavaHungry {
    public static void main(String args[]) {
      StringBuilder str = new StringBuilder("JavaHungry");
      /*Adding character at any position
      using StringBuilder insert() function */
      str.insert(6,'A');
      System.out.println(str); 
    }
}

Output:
JavaHuAngry

3. Using the substring() method

a. Beginning

Insert a character at the beginning of the String using String's substring() method.

public class JavaHungry {
    public static void main(String args[]) {
      String str = "JavaHungry";
      /*Adding character at the beginning
      using String substring() function */
      str = str.substring(0,0) + 'A' + str.substring(0); 
      System.out.println(str); 
    }
}

Output:
AJavaHungry

b. End

Insert a character at the end of the String using String's substring() method.

public class JavaHungry {
    public static void main(String args[]) {
      String str = "JavaHungry";
      /*Adding character at the end
      using String substring() function */
      str = str.substring(0, str.length()) + 'A';
      System.out.println(str); 
    }
}

Output:
JavaHungryA

c. At any position 

Insert a character at any position of the String using String's substring() method.

public class JavaHungry {
    public static void main(String args[]) {
      String str = "JavaHungry";
      /*Adding character at any position
      using String substring() function */
      str = str.substring(0, 5) + 'A' + str.substring(5);
      System.out.println(str); 
    }
}

Output:
JavaHAungry

That's all for today, please mention in the comments in case you know any other way to add a char to a String in java.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry