
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java Program to Insert a string into another string
In this article, we will understand how to insert a string into another string. String is a datatype that contains one or more characters and is enclosed in double quotes(“ ”).
Below is a demonstration of the same −
Suppose our input is −
First string is defined as: Java Program Second string: Learning String to be inserted at index: 0
The desired output would be −
The result is: LearningJava Program
Algorithm
Step 1 - START Step 2 - Declare two strings namely input_string_1, input_string_2, declare a string object namely result. Step 3 - Define the values. Step 4 - Iterate over the first string using a for-loop, Concat the two strings using an arithmetic operator at the ‘i’th position of the first string. Step 5 - Display the result Step 6 - Stop
Example 1
Here, we bind all the operations together under the ‘main’ function.
public class StringInsert { public static void insertString( String input_string_1, String input_string_2, int index) { } public static void main(String[] args) { String input_string_1 = " Java Program"; String input_string_2 = "Learning"; int index = 0; System.out.println("The first string is defined as: " + input_string_1); System.out.println("The second string is defined as: " + input_string_2); System.out.println("String to be inserted at index: " + index); System.out.println("The result is: "); String result = new String(); for (int i = 0; i < input_string_1.length(); i++) { result += input_string_1.charAt(i); if (i == index) { result += input_string_2; } } System.out.println(result); } }
Output
The first string is defined as: Java Program The second string is defined as: Learning String to be inserted at index: 0 The result is: LearningJava Program
Example 2
Here, we encapsulate the operations into functions exhibiting object-oriented programming.
import java.lang.*; public class StringInsert { public static void insertString( String input_string_1, String input_string_2, int index) { String result = new String(); for (int i = 0; i < input_string_1.length(); i++) { result += input_string_1.charAt(i); if (i == index) { result += input_string_2; } } System.out.println(result); } public static void main(String[] args) { String input_string_1 = " Java Program"; String input_string_2 = "Learning"; int index = 0; System.out.println("The first string is defined as: " + input_string_1); System.out.println("The second string is defined as: " + input_string_2); System.out.println("String to be inserted at index: " + index); System.out.println("The result is: "); insertString(input_string_1, input_string_2, index); } }
Output
The first string is defined as: Java Program The second string is defined as: Learning String to be inserted at index: 0 The result is: LearningJava Program
- Related Articles
- Golang program to insert a string into another string
- Insert a String into another String in Java
- Java program to insert a String into a Substring of StringBuffer
- How to insert a string in beginning of another string in java?
- How to copy a String into another String in C#
- String Transforms Into Another String in Python
- Java Program to construct one String from another
- Java Program to Convert a String into the InputStream
- Program to check whether one string can be 1-to-1 mapped into another string in Python
- Java program to convert a list of characters into a string
- Java Program to check whether one String is a rotation of another.
- Print all possible ways to convert one string into another string in C++
- Java program to insert a component into a JTextPane component
- Java Program to Convert the ArrayList into a string and vice versa
- Java Program to convert a string into a numeric primitive type using Integer.valueOf()

Advertisements