如何在 Go 中將字串轉換為標題大小寫?
Title() 是 Go 中 strings 包中的一個內建函式,用於將字串轉換為標題大小寫。它將給定字串中每個單詞的第一個字元轉換為大寫,並返回修改後的字串。
語法
func Title(s string) string
其中 s 是給定的字串。
示例 1
我們來考慮以下示例 -
package main import ( "fmt" "strings" ) func main() { // Intializing the Strings m := "title string function" n := "Golang string package fUNCTION" // Display the Strings fmt.Println("String 1:", m) fmt.Println("String 2:", n) // Using the Title Function res1 := strings.Title(m) res2 := strings.Title(n) // Display the Title Output fmt.Println("\nString 1 in Title Case:", res1) fmt.Println("String 2 in Title Case:", res2) }
輸出
它將生成以下輸出 -
String 1: title string function String 2: Golang string package fUNCTION String 1 in Title Case: Title String Function String 2 in Title Case: Golang String Package FUNCTION
示例 2
我們再舉另一個例子 -
package main import ( "fmt" "strings" ) func main() { // Intializing the Strings x := "syed@123 123 cDE#456" y := "!hello! $$$$world###" // Display the Strings fmt.Println("String 1:", x) fmt.Println("String 2:", y) // Using the Title Function test1 := strings.Title(x) test2 := strings.Title(y) // Display the Title Output fmt.Println("\nString 1 in Title Case:", test1) fmt.Println("String 2 in Title Case:", test2) }
輸出
它將生成以下輸出 -
String 1: syed@123 123 cDE#456 String 2: !hello! $$$$world### String 1 in Title Case: Syed@123 123 CDE#456 String 2 in Title Case: !Hello! $$$$World###
廣告