¿Cómo convertir una string en mayúsculas en Golang?

En el lenguaje Go, las strings son diferentes de otros lenguajes como Java , C++ , Python , etc. Es una secuencia de caracteres de ancho variable donde todos y cada uno de los caracteres están representados por uno o más bytes usando la codificación UTF-8.
En Go string, puede convertir una string en mayúsculas usando las siguientes funciones. Todas estas funciones se definen en el paquete de strings, por lo que debe importar el paquete de strings en su programa para acceder a estas funciones:

1. ToUpper: esta función se utiliza para convertir los elementos de string dados a mayúsculas. O, en otras palabras, esta función devuelve una copia de la string dada en la que todos los caracteres Unicode se asignan a mayúsculas.

Sintaxis:

func ToUpper(str string) string

Aquí, str representa la string que desea convertir a mayúsculas. Analicemos este concepto con la ayuda de un ejemplo:

Ejemplo:

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

Producción:

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: esta función se utiliza para convertir los elementos de string a mayúsculas y minúsculas. O, en otras palabras, esta función devuelve una copia de la string dada en la que todos los caracteres Unicode se asignan al caso del título.

Sintaxis:

func ToTitle(str string) string

Aquí, str representa la string que desea convertir a mayúsculas y minúsculas. Analicemos este concepto con la ayuda de un ejemplo:

Ejemplo:

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

Producción:

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

Publicación traducida automáticamente

Artículo escrito por ankita_saini y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *