Computer >> Computer tutorials >  >> Programming >> Java

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 JLearningava Program