个性化阅读
专注于IT技术分析

在Golang中连接两个字符串的不同方法

在Go语言中, 字符串是使用UTF-8编码编码的不可变的任意字节链。在Go字符串中, 将两个或多个字符串添加到新的单个字符串中的过程称为串联。连接Go语言中两个或多个字符串的最简单方法是使用+运算符。也称为串联运算符。

例子:

//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 = "srcmini"
  
     //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)
  
}

输出如下:

New string 1:  Welcome!srcmini
New string 2:  srcmini

连接字符串的其他方法

使用bytes.Buffer:你也可以通过使用bytes来连接字符串的字节来创建一个字符串。带有WriteString()方法的缓冲区。它在bytes包下定义。它防止生成不必要的字符串对象,这意味着它不会从两个或多个字符串生成一个新的字符串,就像in +操作符那样。

例子:

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

输出如下:

String:  Geeks
String:  srcmini

使用Sprintf:在Go语言中,你也可以使用Sprintf()方法连接字符串。

例子:

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

输出如下:

TutorialofGoLanguage

使用+=操作符或字符串追加:在Go字符串中,允许使用+=操作符追加字符串。此操作符将一个新的或给定的字符串添加到指定字符串的末尾。

例子:

//Go program to illustrate how
//to concatenate strings
//Using += operator
package main
  
import "fmt"
  
func main() {
  
     //Creating and initializing strings
     str1 := "Welcome"
     str2 := "srcmini"
  
     //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)
  
}

输出如下:

String:  Welcomesrcmini
String:  WelcomesrcminiThis is the tutorial of Go language
String:  srcminiPortal

使用Join()函数:

此函数将字符串切片中存在的所有元素连接为单个字符串。此功能在字符串包中可用。

语法如下:

func Join(str []string, sep string) string

这里, str是可用来连接元素的字符串, sep是放置在最终字符串中元素之间的分隔符。

例子:

//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" , "srcmini" , "Portal" }
  
     //Concatenating the elements 
     //present in the slice
     //Using join() function
     result := strings.Join(myslice, "-" )
     fmt.Println(result)
}

输出如下:

Welcome-To-srcmini-Portal

赞(0) 打赏
未经允许不得转载:srcmini » 在Golang中连接两个字符串的不同方法
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

觉得文章有用就打赏一下文章作者

微信扫一扫打赏