How To Add A Char To A String In Java

Java Hungry

Java developers tutorials and coding.

  • HOME
  • STRING
  • COLLECTIONS
  • INTERVIEW
  • EXCEPTION
  • DATA-STRUCTURES
  • OOPS
  • THREADS
  • BOOKS/COURSES
  • JAVA CODING PROGRAM
  • MORE JAVA TOPICS
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

Get new posts by email:
Subscribe

WHATS HOT

  • Best Laptops for Programming
  • Best Books for Learning Java
  • Amazon Interview Question : First Non repeated character in String
  • Count total number of times each alphabet appears in the string java program code with example
  • Java 8 new features : Lambda expressions , optional class , Defender methods with examples
Subscribe for Our Newsletter

POPULAR POSTS

  • Java Interview Questions
  • Top 50 Java Collections Interview Questions and Answers
  • Web Services Interview Questions
  • Java Multithreading Interview Questions and Answers
  • Java Interview Programs
Return to Top ↑ Affiliate-Disclaimer Privacy Contact Java Hungry Copyright © 2025 · All Rights Reserved. Designed by studiopress

Tag » Add Char To String C Sharp