- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to Add Characters to a String
Java provides different ways to add characters to a string. We can add characters in the beginning or at the end or at any position according to the need.
In this article we are going to see three approaches to do so −
Using ‘+’ operator
Using methods of StringBuffer and StringBuilder class
Using substring() method
Approach 1: Using ‘+’ Operator
In this section, we will add a character to a string using the concatenation operator.
Example
public class Main { public static void main(String[] args) { String st = "Tutorial"; char chartoadd = 's'; st = st + chartoadd; // performing concatenation System.out.println(st); } }
Output
Tutorials
Approach 2: Using methods of StringBuilder and StringBuffer class
To understand this section, you need to have a basic understanding of the StringBuilder and StringBuffer classes of Java.
Strings are immutable in java but to perform modification of string, Java provides two inbuilt classes StringBuilder and StringBuffer. Both have insert() and append() methods that can be used to add characters to a string.
insert()
It is present in both classes with a slight difference in that we have to create an object of StringBuilder if we are using the StringBuilder class and create an object of StringBuffer if we are using the StringBuffer class.
The insert() method allow us to add characters at the specified position.
Syntax
public StringBuilder insert(index, char); public StringBuffer insert(index, char);
Parameters
index − position in the string where you want to add the character.
char − character you want to add.
For StringBuffer
Example
public class Main{ public static void main(String []args){ String st="oint"; char chartoadd='P'; StringBuffer new_st=new StringBuffer(st); new_st.insert(0,chartoadd); System.out.println(new_st); } }
Output
Point
‘point’ is the name of the string and ‘P’ is the character we want to add at 0th position. First, we have created an object named ‘new_st’ of StringBuffer class and then we have used insert() method with index value 0 and name of variable that holds character ‘P’.
For StringBuilder
Example
public class Main{ public static void main(String []args){ String st="Tutorial"; char chartoadd='s'; StringBuilder new_st=new StringBuilder(st); new_st.insert(8,chartoadd); System.out.println(new_st); } }
Output
Tutorials
‘Tutorial’ is the name of given string and ‘s’ is the character we want to add at the 8th position. First, we have created an object named ‘new_st’ of StringBuilder class and then we have used insert() method with index value 8 and name of variable that holds character ‘s’.
append()
It is also present in both classes with a slight difference in that we have to create an object of StringBuilder if we are using StringBuilder class and create an object of StringBuffer if we are using StringBuffer class.
The append() method will add characters at the end of string.
Syntax
public StringBuffer append(char); public StringBuilder append(char);
Parameter
char − character you want to add.
For StringBuffer
public class Main{ public static void main(String []args){ String st="Simpl"; char chartoadd='y'; StringBuffer new_st=new StringBuffer(st); new_st.append(chartoadd); System.out.println(new_st); } }
Output
Simply
Here, we have created an object named ‘new_st’ of StringBuffer class and then we have used append() method with name of variable that holds character ‘y’. Remember, append() method doesn’t require index value like insert() method.
For StringBuilder
Example
public class Main{ public static void main(String []args){ String st="Tutorial"; char chartoadd='s'; StringBuilder new_st=new StringBuilder(st); new_st.append(chartoadd); System.out.println(new_st); } }
Output
Tutorials
We have created an object named ‘new_st’ of StringBuilder class and then we have used append() method with name of variable that holds character ‘s’.
Approach 3: Using substring() method of String class
The String class of Java has a method called substring() that can be used to add characters to a given string.
Syntax
old_string.substring(start_pos, target_pos);
Parameters
start_pos − Start index of the old string
target_pos − Index where you want to add the character.
Example
public class Main{ public static void main(String []args){ String st="Tutorialsoint"; char chartoadd='P'; String new_st= st.substring(0,9) + chartoadd + st.substring(9); System.out.println(new_st); } }
Output
TutorialsPoint
We have created ‘new_st’ as a new concatenated string. First, we have splitted the old string ‘st’ from 0th position to 8th position and then we have added the character at 9th position. At the end, we have added rest of the string.
Conclusion
We have seen different methods to add characters to a string. We can use the concatenation operator, insert() and append() methods. The StringBuilder class is faster than the StringBuffer class but for thread safety, StringBuffer is more secure. If we want to add characters at a specific position then we can prefer insert() method and for adding a character at the end of a string use append() method. At last, we have seen the use substring() method that requires dividing the old string into different parts.