Rôzne spôsoby, ako spojiť dva reťazce v Golang

V jazyku Go je reťazec nemenným reťazcom ľubovoľných bajtov kódovaných kódovaním UTF-8. V reťazcoch Go je proces pridávania dvoch alebo viacerých reťazcov do nového jedného reťazca známy ako zreťazenie. Najjednoduchším spôsobom zreťazenia dvoch alebo viacerých reťazcov v jazyku Go je použitie operátora +. Je tiež známy ako operátor zreťazenia.

Príklad:

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)> }>

Výkon:

New string 1: Welcome!techcodeview.com New string 2: techcodeview.com 

Iné metódy na zreťazenie reťazcov

    Použitie bytes.Buffer: Reťazec môžete vytvoriť aj zreťazením bajtov reťazcov pomocou bytes.Buffer s Metóda WriteString(). . Je definovaný v balíku bajtov. Zabraňuje generovaniu nepotrebného objektu typu reťazec, čo znamená, že negeneruje nový reťazec ako v operátore + z dvoch alebo viacerých reťazcov. Príklad:

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())> }>

    Výkon:
String: Geeks String: techcodeview.com 
    Použitie Sprintf: V jazyku Go môžete reťazec zreťaziť aj pomocou Sprintf() metóda. Príklad:

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)> }>

    Výkon:
TutorialofGoLanguage 
    Použitie operátora += alebo pripojenia reťazca: V reťazcoch Go môžete pridať reťazec pomocou operátor += . Tento operátor pridá nový alebo daný reťazec na koniec zadaného reťazca. Príklad:

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)> }>

    Výkon:
String: Welcometechcodeview.com String: Welcometechcodeview.comThis is the tutorial of Go language String: techcodeview.comPortal 
    Použitie funkcie Join(): Táto funkcia spája všetky prvky prítomné v reze reťazca do jedného reťazca. Táto funkcia je dostupná v balíku reťazcov. Syntax:
func Join(str []string, sep string) string 
  • Tu, str je reťazec, z ktorého môžeme zreťaziť prvky a sep je oddeľovač, ktorý sa umiestni medzi prvky v konečnom reťazci. Príklad:

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)> }>

    Výkon:
Welcome-To-techcodeview.com-Portal 
    Pomocou strings.Builder: Reťazec môžete vytvoriť aj zreťazením reťazcov pomocou strings.Builder s Metóda WriteString(). Je definovaný v balíku reťazcov. Pri spájaní reťazcov využíva menej pamäte a je to lepší spôsob spájania. Funkcia WriteString pripája obsah reťazca do vyrovnávacej pamäte a umožňuje nám rýchlejšie spájať reťazce. Zabraňuje generovaniu nepotrebného objektu typu reťazec, čo znamená, že negeneruje nový reťazec ako v operátore + z dvoch alebo viacerých reťazcov.

Príklad:

Choď




// 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())> }>

Výkon:

String: Welcome String: Welcome to techcodeview.com!