logo

Různé způsoby, jak zřetězit dva řetězce v Golang

V jazyce Go je řetězec neměnným řetězcem libovolných bajtů kódovaných kódováním UTF-8. V řetězcích Go je proces přidávání dvou nebo více řetězců do nového jediného řetězce známý jako zřetězení. Nejjednodušším způsobem zřetězení dvou nebo více řetězců v jazyce Go je použití operátoru +. Je také známý jako operátor zřetězení.

Příklad:



C




jak proměnit řetězec v int





// 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ýstup:

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

Další metody pro zřetězení řetězců

    Použití bytes.Buffer: Můžete také vytvořit řetězec zřetězením bajtů řetězců pomocí bytes.Buffer s Metoda WriteString(). . Je definována v balíčku bajtů. Zabraňuje generování zbytečného objektu typu string, což znamená, že negeneruje nový řetězec jako v operátoru + ze dvou nebo více řetězců. Příklad:

C


css změna velikosti obrázku



// 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ýstup:
String: Geeks String: techcodeview.com>
    Použití Sprintf: V jazyce Go můžete také zřetězit řetězec pomocí Sprintf() metoda. Pří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ýstup:
TutorialofGoLanguage>
    Použití operátoru += nebo String append: V řetězcích Go je povoleno připojit řetězec pomocí += operátor . Tento operátor přidá nový nebo daný řetězec na konec zadaného řetězce. Pří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)> }>

>

>

formát řetězce v jazyce Java
    Výstup:
String: Welcometechcodeview.com String: Welcometechcodeview.comThis is the tutorial of Go language String: techcodeview.comPortal>
    Použití funkce Join(): Tato funkce zřetězí všechny prvky přítomné v řezu řetězce do jednoho řetězce. Tato funkce je dostupná v řetězcovém balíčku. Syntax:
func Join(str []string, sep string) string>
  • Tady, str je řetězec, ze kterého můžeme zřetězit prvky a sep je oddělovač, který je umístěn mezi prvky ve finálním řetězci. Pří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ýstup:
Welcome-To-techcodeview.com-Portal>
    Použití strings.Builder: Můžete také vytvořit řetězec zřetězením řetězců pomocí strings.Builder with Metoda WriteString(). Je definován pod balíčkem řetězců. Při zřetězení řetězců využívá méně paměti a je to lepší způsob zřetězení. Funkce WriteString připojí obsah řetězce do vyrovnávací paměti a umožní nám zřetězit řetězce rychlejším způsobem. Zabraňuje generování zbytečného objektu typu string, což znamená, že negeneruje nový řetězec jako v operátoru + ze dvou nebo více řetězců.

Příklad:

Jít




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

>

jak převést int na řetězec
>

Výstup:

String: Welcome String: Welcome to techcodeview.com!>