
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Add Characters to a String in Java
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 different approaches to do so:
-
Using '+' operator
-
Using methods of StringBuffer and StringBuilder classes
-
Using substring() method
Using the '+' Operator
In this section, we will add a character to a string using the concatenation operator. On adding the character to the string, it will automatically be converted to a String and appended.
Example
Let's see an example of adding a character to a string using the '+' operator.
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
Using StringBuilder and StringBuffer
Strings are immutable in Java, but to perform modifications of strings, Java provides two inbuilt classes, StringBuilder and StringBuffer. Both have insert() and append() methods that can be used to add characters to a string.
The insert() Method
The insert() method is used to add given elements at a specified position. It is present in both classes. But, the difference is 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.
Syntax
public StringBuilder insert(index, char); Or, public StringBuffer insert(index, char);
Where,
The index is a position in the string where you want to add the character. And, char is the character you want to add.
Example
In the following Java program, we will use the insert() method of the StringBuffer class.
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
The append() Method
The append() method will add characters to the end of a string. Like insert(), it is also present in both classes.
Syntax
public StringBuffer append(char); Or, public StringBuilder append(char);
Where char is the character you want to add.
Example
In the following Java program, we will use the append() method of StringBuilder class.
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
Using the substring() Method
The String class of Java has a method called substring() that can be used to add characters to a given string. It will return a new String after adding the character. Following is the syntax of this method -
old_string.substring(start_pos, target_pos);
Where,
-
start_pos: Start index of the old string
-
target_pos: Index where you want to add the character.
Example
The following Java program uses the substring() method to add a new character to the String. The character will be added to the 9th index.
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