Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert a string inside another string using Go lang

Tags:

string

go

Most programming languages have a function that allows us to insert one string into another string. For example, I can take the string Green and the string HI, and perform an operation Green.insert(HI,2) to get the resulatant string GrHIeen. But such a function does not come with the standard GO lang library.

Is there any Golang function which I can use to insert a string inside an string?

For example

string = "</table></body></html>"

// I want Following Output

string = "</table><pagebreak /></body></html>"
like image 358
Ali Avatar asked Oct 03 '16 10:10

Ali


People also ask

Is string in string Golang?

Golang String Contains() is a built-in function that checks whether substr is within the string. The Contains() function accepts two arguments and returns the boolean value, either true or false.

What does string () do in Golang?

In Go language, strings are different from other languages like Java, C++, Python, etc. it is a sequence of variable-width characters where each and every character is represented by one or more bytes using UTF-8 Encoding.

How do I find a substring in a string in Golang?

Golang String Index() To find the index of a particular substring of string In Go, use the Index() function. The Index() function accepts three arguments and returns the index of the substring. To use the Index() function, you need to import the strings package in your program for accessing the Index() function.

How to insert one string into another string in Golang?

Most programming languages have a function that allows us to insert one string into another string. For example, I can take the string Green and the string HI, and perform an operation Green.insert (HI,2) to get the resulatant string GrHIeen. But such a function does not come with the standard GO lang library.

What is a string in Go language?

In Go language, the string is an immutable chain of arbitrary bytes encoded with UTF-8 encoding. In Go strings, the process of adding two or more strings into a new single string is known as concatenation.

How to concatenate strings in Go language?

In Go language, the string is an immutable chain of arbitrary bytes encoded with UTF-8 encoding. In Go strings, the process of adding two or more strings into a new single string is known as concatenation. The simplest way of concatenating two or more strings in the Go language is by using + operator.

How to append a string to a string in go?

Using += operator or String append: In Go strings, you are allowed to append a string using += operator. This operator adds a new or given string to the end of the specified string.


1 Answers

You can simply use slice operations on the string:

package main

func main() {
    p := "green"
    index := 2
    q := p[:index] + "HI" + p[index:]
    fmt.Println(p, q)
}

Working example: https://play.golang.org/p/01phuBKuBB

like image 188
abhink Avatar answered Oct 13 '22 00:10

abhink