How To Add A Char To A String In Java
Maybe your like
Java Hungry
Java developers tutorials and coding.
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 Java1. 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: JavaHungry2. 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: JavaHuAngry3. 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:
SubscribeWHATS 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
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
Tag » Add Char To String C Sharp
-
C# Adding A Character In A String - Stack Overflow
-
Add Character To String C# Code Example - Code Grepper
-
String.Insert(Int32, String) Method (System) - Microsoft Docs
-
Append A Char To End Of A String In C# | Techie Delight
-
C# - How To Append A Char To A String - 1400+ .NET C# Examples
-
C# Add Strings - ZetCode
-
6 Effective Ways To Concatenate Strings In C# - C# Corner
-
How To Append A Character To A String In C - GeeksforGeeks
-
How Do I Add A Char To A String? - Unity Forum
-
How To Concatenate Chars To String In C# - YouTube
-
Concatenate Char To String In C# - Software Engineers Blogs
-
C# String Append (Add Strings) - Dot Net Perls
-
Add Char To Beginning Of String C# Code Example - Newbedev
-
C# StringBuilder Append(Char[]) Array