We will discuss how to add characters to a string at a specific position in a string in java. It can be explained as follows, as shown in the figure, what we are trying to do 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 a number of methods, the most common of which are listed below:
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: The "+" operator can be used to add characters to the beginning of a character string.
// Java Program to Add Characters to a String // At the End // Importing input output classes import java.io.*; // Main class public 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); } }
Output
GeeksforGeeks
1.2 At the beginning
Example: One can add character at the start of String using the ‘+’ operator.
// Java Program to Add Characters to a String // At the Beginning // Importing input output classes import java.io.*; // Main class public 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); } }
Output
GeeksforGeeks
Method 2: Using insert() method of StringBuffer class
StringBuffer is the equivalent of String, and it provides most of the functionality of strings. String represents a fixed-length, immutable sequence of characters, and StringBuffer represents a sequence of characters that can grow and be writable. StringBuffer may insert characters and substrings in the middle or append to the end. It grows automatically to make room for such additions, and it usually pre-allocates more characters than actually needed to make room for growth. You can use the StringBuffer class method, the insert() method, to add characters to the String at a given position. This method inserts the string representation of the given data type at the given position in the 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 Program to Add Characters to a String // Using StringBuffer class insert() method // Main class // AddCharacterToStringAnyPosition public 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); } }
Output
GeeksforGeeks
Method 3: Using substring() method
You can also use String's substring method to add characters to a String at a given position. There are two variants of this method. It returns a new string that is a substring of the string, where the substring starts with the 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 Program to Add Characters to a String // Using substring() method // Main class // AddCharacterToStringAnyPosition public 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); } }
Output
GeeksforGeeks
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.