📌  相关文章
📜  如何在Golang中将字符串转换为大写?

📅  最后修改于: 2021-10-25 02:12:27             🧑  作者: Mango

在 Go 语言中,字符串不同于Java、C++、 Python等其他语言。它是一系列可变宽度字符,其中每个字符都由一个或多个使用 UTF-8 编码的字节表示。
在 Go 字符串,您可以使用以下函数将字符串转换为大写。所有这些函数都定义在字符串包下,因此您必须在程序中导入字符串包才能访问这些函数:

1. ToUpper:该函数用于将给定的字符串元素转换为大写。或者换句话说,此函数返回给定字符串的副本,其中所有 Unicode字符都映射为大写。

句法:

func ToUpper(str string) string

在这里, str表示要转换为大写的字符串。让我们借助一个例子来讨论这个概念:

例子:

// Go program to illustrate how to convert
// the given string into uppercase
package main
  
import (
    "fmt"
    "strings"
)
  
// Main method
func main() {
  
    // Creating and initializing string
    // Using shorthand declaration
    str1 := "WelcomE, GeeksforGeeks**"
    str2 := "$$This is the, tuTorial oF Golang##"
    str3 := "HELLO! GOLANG"
    str4 := "uppercase conversion"
  
    // Displaying strings
    fmt.Println("Strings (before):")
    fmt.Println("String 1: ", str1)
    fmt.Println("String 2:", str2)
    fmt.Println("String 3:", str3)
    fmt.Println("String 4:", str4)
  
    // Converting all the string into uppercase
    // Using ToUpper() function
    res1 := strings.ToUpper(str1)
    res2 := strings.ToUpper(str2)
    res3 := strings.ToUpper(str3)
    res4 := strings.ToUpper(str4)
  
    // Displaying the results
    fmt.Println("\nStrings (after):")
    fmt.Println("Result 1: ", res1)
    fmt.Println("Result 2:", res2)
    fmt.Println("Result 3:", res3)
    fmt.Println("Result 4:", res4)
}

输出:

Strings (before):
String 1:  WelcomE, GeeksforGeeks**
String 2: $$This is the, tuTorial oF Golang##
String 3: HELLO! GOLANG
String 4: uppercase conversion

Strings (after):
Result 1:  WELCOME, GEEKSFORGEEKS**
Result 2: $$THIS IS THE, TUTORIAL OF GOLANG##
Result 3: HELLO! GOLANG
Result 4: UPPERCASE CONVERSION

2. ToTitle:该函数用于将字符串元素转换为标题大小写。或者换句话说,此函数返回给定字符串的副本,其中所有 Unicode字符都映射到标题大小写中。

句法:

func ToTitle(str string) string

这里, str表示要转换为标题大小写的字符串。让我们借助一个例子来讨论这个概念:

例子:

// Go program to illustrate how to convert
// the given string into title case
package main
  
import (
    "fmt"
    "strings"
)
  
// Main method
func main() {
  
    // Creating and initializing string
    // Using shorthand declaration
    str1 := "WelcomE, GeeksforGeeks**"
    str2 := "$$This is the, tuTorial oF Golang##"
    str3 := "HELLO! GOLANG"
    str4 := "uppercase conversion"
  
    // Displaying strings
    fmt.Println("Strings (before):")
    fmt.Println("String 1: ", str1)
    fmt.Println("String 2:", str2)
    fmt.Println("String 3:", str3)
    fmt.Println("String 4:", str4)
  
    // Converting all the string into title case
    // Using ToTitle() function
    res1 := strings.ToTitle(str1)
    res2 := strings.ToTitle(str2)
    res3 := strings.ToTitle(str3)
    res4 := strings.ToTitle(str4)
  
    // Displaying the results
    fmt.Println("\nStrings (after):")
    fmt.Println("Result 1: ", res1)
    fmt.Println("Result 2:", res2)
    fmt.Println("Result 3:", res3)
    fmt.Println("Result 4:", res4)
}

输出:

Strings (before):
String 1:  WelcomE, GeeksforGeeks**
String 2: $$This is the, tuTorial oF Golang##
String 3: HELLO! GOLANG
String 4: uppercase conversion

Strings (after):
Result 1:  WELCOME, GEEKSFORGEEKS**
Result 2: $$THIS IS THE, TUTORIAL OF GOLANG##
Result 3: HELLO! GOLANG
Result 4: UPPERCASE CONVERSION