Java Program to Add Characters to a String - GeeksforGeeks

16 Jul, 2021
We will be discussing out how to add character to a string at particular position in a string in java. It can be interpreted as follows as depicted in the illustration what we are trying to do which is as follows:
Illustration:
Input: 
Input custom string = Hello
Output: 
--> String to be added 'Geeks'
    --> If end position, Output: HelloGeeks
    --> If in beginning, Output: GeeksHello
    --> If at sat 3rd index, Output: HelGeekslo 
Methods: This can be done using multiple methods of which frequently used methods are listed below as follows:
Using + operatorAt the endAt the beginningUsing insert() method of StringBuffer classUsing substring() method
Using + operatorAt the endAt the beginning
At the end
At the beginning
Using insert() method of StringBuffer class
Using substring() method
Let us discuss all three methods above listed in detail to get a fair understanding of the same
Method 1: Using + operator
1.1 At the end
Example: One can add character at the start of String using the ‘+’ operator.
Java
// Java Program to Add Characters to a String// At the End // Importing input output classesimport java.io.*; // Main classpublic class GFG {     // Main driver method    public static void main(String args[])    {         // Input character and string        char a = 's';        String str = "GeeksforGeek";         // Inserting at the end        String str2 = str + a;         // Print and display the above string        System.out.println(str2);    }}
GeeksforGeeks
1.2 At the beginning
Example: One can add character at the start of String using the ‘+’ operator. 
Java
// Java Program to Add Characters to a String// At the Beginning // Importing input output classesimport java.io.*; // Main classpublic class GFG {     // Main driver method    public static void main(String args[])    {        // Input character and string        char a = 'G';        String str = "eeksforGeeks";         // Inserting at the beginning        String str2 = a + str;         // Print and display the above string        System.out.println(str2);    }}
GeeksforGeeks
Method 2: Using insert() method of StringBuffer class 
StringBuffer is a peer class of String that provides much of the functionality of strings. The string represents fixed-length, immutable character sequences while StringBuffer represents grow able and writable character sequences. StringBuffer may have characters and sub-strings inserted in the middle or appended to the end. It will automatically grow to make room for such additions and often has more characters pre-allocated than are actually needed, to allow room for growth. One can use the StringBuffer class method namely the insert() method to add character to String at the given position. This method inserts the string representation of given data type at given position in StringBuffer.
Syntax: 
 str.insert(int position,  char x);
 str.insert(int position,  boolean x);
 str.insert(int position,  char[] x);
 str.insert(int position, float x);
 str.insert(int position, double x);
 str.insert(int position, long x);
 str.insert(int position, int x);

position is the index in string where
we need to insert.
Return type: A reference to this object.
Example  
Java
// Java Program to Add Characters to a String// Using StringBuffer class insert() method // Main class// AddCharacterToStringAnyPositionpublic class GFG {     // Method 1    // To add character to string    public static String addCharToString(String str, char c,                                         int pos)    {         // Creating an object of StringBuffer class        StringBuffer stringBuffer = new StringBuffer(str);         // insert() method where position of character to be        // inserted is specified as in arguments        stringBuffer.insert(pos, c);         // Return the updated string        // Concatenated string        return stringBuffer.toString();    }     // Method 2    // Main driver method    public static void main(String[] args)    {         // Input string and character        String blogName = "GeeksorGeeks";        char two = 'f';         // Calling the method 1 to        // add character to a string         // Custom string, character and position passed        String cblogName            = addCharToString(blogName, two, 5);         // Print and display th above string        System.out.println(cblogName);    }}
GeeksforGeeks
Method 3: Using substring() method
One can also use String’s substring method to add character to String at given position. This method has two variants, and it returns a new string hat is substring of a string where the substring begins with a character at the specified index and extends to the end of the string.  
Syntax:  
public String substring(int begIndex)
Parameters: The beginning index, inclusive.
Return Value: The specified substring.
Example  
Java
// Java Program to Add Characters to a String// Using substring() method // Main class// AddCharacterToStringAnyPositionpublic class GFG {     // Method 1    // To add character to a string    public static String    addCharToStringUsingSubString(String str, char c,                                  int pos)    {        return str.substring(0, pos) + c            + str.substring(pos);    }     // Method 2    // Main driver method    public static void main(String[] args)    {        // Custom input character and string        String blogName = "GeeksorGeeks";        char two = 'f';         // Calling the Method 1 to        // To add character to a string         // Custom arguments        String cblogName = addCharToStringUsingSubString(            blogName, two, 5);         // Print and display the above string on console        System.out.println(cblogName);    }}
GeeksforGeeks
 
akshaysingh98088
sagar0719kumar
surindertarika1234
Java-String-Programs
Picked
Java
Java Programs
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Interfaces in Java
Initialize an ArrayList in Java
ArrayList in Java
Multidimensional Arrays in Java
Overriding in Java
Initializing a List in Java
Convert a String to Character array in Java
Java Programming Examples
Implementing a Linked List in Java using Class
Convert Double to Integer in Java