Diversi modi per concatenare due stringhe in Golang
Nel linguaggio Go, la stringa è una catena immutabile di byte arbitrari codificati con codifica UTF-8. Nelle stringhe Go, il processo di aggiunta di due o più stringhe in una nuova stringa singola è noto come concatenazione. Il modo più semplice per concatenare due o più stringhe nel linguaggio Go è utilizzare l'operatore +. È noto anche come operatore di concatenazione.
Esempio:
C
// Go program to illustrate> // how to concatenate strings> package main> import 'fmt'> func main() {> > // Creating and initializing strings> > // using var keyword> > var str1 string> > str1 = 'Welcome!'> > var str2 string> > str2 = 'techcodeview.com'> > // Concatenating strings> > // Using + operator> > fmt.Println('New string 1: ', str1+str2)> > // Creating and initializing strings> > // Using shorthand declaration> > str3 := 'Geeks'> > str4 := 'Geeks'> > // Concatenating strings> > // Using + operator> > result := str3 + '> for> ' + str4> > fmt.Println('New string 2: ', result)> }> |
Produzione:
New string 1: Welcome!techcodeview.com New string 2: techcodeview.com
Altri metodi per concatenare stringhe
- Utilizzando bytes.Buffer: puoi anche creare una stringa concatenando i byte delle stringhe utilizzando bytes.Buffer con Metodo WriteString() . È definito nel pacchetto bytes. Impedisce la generazione dell'oggetto stringa non necessario, significa che non genera una nuova stringa come nell'operatore + da due o più stringhe. Esempio:
C
// Go program to illustrate how to concatenate strings> // Using bytes.Buffer with WriteString() function> package main> import (> > 'bytes'> > 'fmt'> )> func main() {> > // Creating and initializing strings> > // Using bytes.Buffer with> > // WriteString() function> > var b bytes.Buffer> > > b.WriteString('G')> > b.WriteString('e')> > b.WriteString('e')> > b.WriteString('k')> > b.WriteString('s')> > > fmt.Println('String: ', b.String())> > b.WriteString('f')> > b.WriteString('o')> > b.WriteString('r')> > b.WriteString('G')> > b.WriteString('e')> > b.WriteString('e')> > b.WriteString('k')> > b.WriteString('s')> > > fmt.Println('String: ', b.String())> }> |
- Produzione:
String: Geeks String: techcodeview.com
- Usando Sprintf: nel linguaggio Go, puoi anche concatenare la stringa usando Sprintf() metodo. Esempio:
C
// Go program to illustrate how to concatenate strings> // Using Sprintf function> package main> import 'fmt'> func main() {> > // Creating and initializing strings> > str1 := 'Tutorial'> > str2 := 'of'> > str3 := 'Go'> > str4 := 'Language'> > // Concatenating strings using> > // Sprintf() function> > result := fmt.Sprintf('%s%s%s%s', str1,> > str2, str3, str4)> > > fmt.Println(result)> }> |
- Produzione:
TutorialofGoLanguage
- Utilizzo dell'operatore += o aggiunta stringa: nelle stringhe Go, è consentito aggiungere una stringa utilizzando += operatore . Questo operatore aggiunge una stringa nuova o specificata alla fine della stringa specificata. Esempio:
C
// Go program to illustrate how> // to concatenate strings> // Using += operator> package main> import 'fmt'> func main() {> > // Creating and initializing strings> > str1 := 'Welcome'> > str2 := 'techcodeview.com'> > // Using += operator> > str1 += str2> > fmt.Println('String: ', str1)> > str1 += 'This is the tutorial of Go language'> > fmt.Println('String: ', str1)> > str2 += 'Portal'> > fmt.Println('String: ', str2)> }> |
- Produzione:
String: Welcometechcodeview.com String: Welcometechcodeview.comThis is the tutorial of Go language String: techcodeview.comPortal
- Utilizzo della funzione Join(): questa funzione concatena tutti gli elementi presenti nella porzione di stringa in un'unica stringa. Questa funzione è disponibile nel pacchetto string. Sintassi:
func Join(str []string, sep string) string
- Qui, stra è la stringa da cui possiamo concatenare gli elementi e sep è il separatore che viene inserito tra gli elementi nella stringa finale. Esempio:
C
// Go program to illustrate how to> // concatenate all the elements> // present in the slice of the string> package main> import (> > 'fmt'> > 'strings'> )> func main() {> > // Creating and initializing slice of string> > myslice := []string{'Welcome', 'To',> > 'techcodeview.com', 'Portal'}> > // Concatenating the elements> > // present in the slice> > // Using join() function> > result := strings.Join(myslice, '-')> > fmt.Println(result)> }> |
- Produzione:
Welcome-To-techcodeview.com-Portal
- Utilizzando strings.Builder: puoi anche creare una stringa concatenando le stringhe utilizzando strings.Builder con Metodo WriteString(). È definito nel pacchetto strings. Utilizza meno memoria durante la concatenazione delle stringhe ed è un modo migliore di concatenazione. La funzione WriteString aggiunge il contenuto della stringa a un buffer e ci consente di concatenare le stringhe in modo più veloce. Impedisce la generazione dell'oggetto stringa non necessario, significa che non genera una nuova stringa come nell'operatore + da due o più stringhe.
Esempio:
Andare
// Go program to illustrate how to concatenate strings> // Using strings.Builder with WriteString() function> package> main> import> (> > 'fmt'> > 'strings'> )> func> main() {> > // Creating and initializing strings> > // Using strings.Builder with> > // WriteString() function> > var> str strings.Builder> > str.WriteString(> 'Welcome'> )> > fmt.Println(> 'String: '> , str.String())> > str.WriteString(> ' to'> )> > str.WriteString(> ' techcodeview.com!'> )> > fmt.Println(> 'String: '> , str.String())> }> |
Produzione:
String: Welcome String: Welcome to techcodeview.com!