Golang program to insert a string into another string


In the Go programming language, strings are a built-in data type that represents sequences of characters. They are defined using double quotes (") and can contain any valid one Example characters. A string is added to or positioned within a bigger string when it is "inserted" into another string. This might be accomplished by replacing a specific substring within the bigger string or at a certain position or index within the larger string. This is frequently used in programming when a string needs to be modified or altered in a particular way, for as by adding a prefix or suffix or by replacing specific characters or substrings.

Method 1: Using Slicing Method

In this method, to insert the new string into the original string at the specified location, this program employs string slices.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) package in the program where main produces executable Examples and fmt helps in formatting input and output.

  • Step 2 − Create a main function and in that function declare and initialize the starting string, the new string, and the point where the new string should be inserted.

  • Step 3 − Concatenate the following strings using string slicing to create a new string: a portion of the initial string that spans from the start to the specified place and from the specified place to the end, a portion of the original string.

  • Step 4 − Print the concatenated string on the console using fmt.Println() function where ln means new line.

  • Step 5 − In this illustration, "Hello, developer!" is the original string, "software" is the string that needs to be inserted, and position 7 is where it has to go. The software first concatenates the string to be inserted and slices the original string from 0 to 7; it then slices from 7 to the end of the original string and concatenates all.

Example

In this example, we will learn how to insert a string into another string using slicing.

package main
import (
   "fmt"
)

//create a function main to execute the program
func main() {
   initial_input := "Hello, developer!"  //create an original input string
   fmt.Println("The initial string given here is:", initial_input)
   new_input := "software"  //create a new string which is to be concatenated
   pos := 7
   fmt.Println("The string after new input is added:")
   fmt.Println(initial_input[:pos] + new_input + initial_input[pos:]) //concatenate the strings and print on the console
}

Output

The initial string given here is: Hello, developer!
The string after new input is added:
Hello, softwaredeveloper!

Method 2: Using bytes.Buffer Package

The bytes.Buffer package is used by this program to establish a new buffer, write the string to be inserted, the original string slice from the specified position to the end, and the original string slice up to the specified position. Using the buffer's.String() method, the desired outcome is produced. Let’s see the Example and algorithm to understand the concept.

Syntax

buffer.String()

A bytes-based approach is the String() one. In Go, a growable buffer of bytes is represented by a buffer struct. The buffer's contents are returned as a string by the String() method.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) package in the program where main produces executable Examples and fmt helps in formatting input and output.

  • Step 2 − Create a main function and in that function declare and initialize the starting string, the new string, and the point where the new string should be inserted.

  • Step 3 − Utilize the bytes to make a new buffer.

  • Step 4 − Use the buffer's WriteString() method to add the following strings to the buffer: a portion of the initial string that spans from the start to the specified place and from the specified place to the end, a portion of the original string.

  • Step 5 − To get the finished string with the inserted string, use the buffer's String() method.

  • Step 6 − Publish the finished string using fmt.Println() function where ln means new line.

Example

In this example we will learn how to insert a string into another slice using buffer package.

package main
import (
   "bytes"
   "fmt"
)
//create a function main to execute the program
func main() {
   initial_input := "Hello, developer!"    //create an original string
   fmt.Println("The original string given here is:", initial_input)
   new_input := "software"  //create a new string 
   fmt.Println("The new string to be added here is:", new_input)
   pos := 7

   var buffer bytes.Buffer
   buffer.WriteString(initial_input[:pos])  //add the string to the buffer 
   buffer.WriteString(new_input)
   buffer.WriteString(initial_input[pos:])
   fmt.Println(buffer.String())    //print the concatenated string on the console
}

Output

The original string given here is: Hello, developer!
The new string to be added here is: software
Hello, softwaredeveloper!

Conclusion

We executed the program of inserting a string into another string using two different examples. In the first example we used string slicing and in the second example we used bytes.Buffer package. Both programs give similar output.

Updated on: 20-Feb-2023

697 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements