logo

¿Cómo dividir una cadena en Golang?

En idioma Go, instrumentos de cuerda son diferentes de otros idiomas como Java , C++ , Pitón , 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 utilizando codificación UTF-8. En Go strings, puede dividir una cadena en un segmento con la ayuda de las siguientes funciones. Estas funciones se definen en el paquete de cadenas, por lo que debe importar el paquete de cadenas en su programa para acceder a estas funciones:
1. Dividir: Esta función divide una cadena en todas las subcadenas separadas por el separador dado y devuelve un segmento que contiene estas subcadenas.
Sintaxis:

func Split(str, sep string) []string>

Aquí, cadena es la cadena y sep es el separador. Si cadena no contiene lo dado sep y sep no está vacío, devolverá un segmento de longitud 1 que contiene solo cadena . O si el sep está vacío, se dividirá después de cada secuencia UTF-8. O si ambos cadena y sep están vacíos, entonces devolverá un segmento vacío.
Ejemplo:

Ir






// Go program to illustrate how to split a string> package> main> import> (> >'fmt'> >'strings'> )> // Main function> func> main() {> >// Creating and initializing the strings> >str1 :=>'Welcome, to the, online portal, of techcodeview.com'> >str2 :=>'My dog name is Dollar'> >str3 :=>'I like to play Ludo'> >// Displaying strings> >fmt.Println(>'String 1: '>, str1)> >fmt.Println(>'String 2: '>, str2)> >fmt.Println(>'String 3: '>, str3)> >// Splitting the given strings> >// Using Split() function> >res1 := strings.Split(str1,>','>)> >res2 := strings.Split(str2,>''>)> >res3 := strings.Split(str3,>'!'>)> >res4 := strings.Split(>''>,>'techcodeview.com, geeks'>)> >// Displaying the result> >fmt.Println(>' Result 1: '>, res1)> >fmt.Println(>'Result 2: '>, res2)> >fmt.Println(>'Result 3: '>, res3)> >fmt.Println(>'Result 4: '>, res4)> }>

>

expresión regular java para

>

Producción:

String 1: Welcome, to the, online portal, of techcodeview.com String 2: My dog name is Dollar String 3: I like to play Ludo  Result 1: [Welcome to the online portal of techcodeview.com] Result 2: [M y d o g n a m e i s D o l l a r] Result 3: [I like to play Ludo] Result 4: []>

2. Dividir después: Esta función divide una cadena en todas las subcadenas después de cada instancia del separador dado y devuelve un segmento que contiene estas subcadenas.
Sintaxis:

func SplitAfter(str, sep string) []string>

Aquí, cadena es la cadena y sep es el separador. Si cadena no contiene lo dado sep y sep no está vacío, devolverá un segmento de longitud 1 que contiene solo cadena . O si el sep está vacío, se dividirá después de cada secuencia UTF-8. O si ambos cadena y sep están vacíos, entonces devolverá un segmento vacío.
Ejemplo:

Ir




// Go program to illustrate how to split a string> package> main> import> (> >'fmt'> >'strings'> )> // Main function> func> main() {> >// Creating and initializing the strings> >str1 :=>'Welcome, to the, online portal, of techcodeview.com'> >str2 :=>'My dog name is Dollar'> >str3 :=>'I like to play Ludo'> >// Displaying strings> >fmt.Println(>'String 1: '>, str1)> >fmt.Println(>'String 2: '>, str2)> >fmt.Println(>'String 3: '>, str3)> >// Splitting the given strings> >// Using SplitAfter() function> >res1 := strings.SplitAfter(str1,>','>)> >res2 := strings.SplitAfter(str2,>''>)> >res3 := strings.SplitAfter(str3,>'!'>)> >res4 := strings.SplitAfter(>''>,>'techcodeview.com, geeks'>)> >// Displaying the result> >fmt.Println(>' Result 1: '>, res1)> >fmt.Println(>'Result 2: '>, res2)> >fmt.Println(>'Result 3: '>, res3)> >fmt.Println(>'Result 4: '>, res4)> }>

tabla de reacción

>

>

Producción:

String 1: Welcome, to the, online portal, of techcodeview.com String 2: My dog name is Dollar String 3: I like to play Ludo  Result 1: [Welcome, to the, online portal, of techcodeview.com] Result 2: [M y d o g n a m e i s D o l l a r] Result 3: [I like to play Ludo] Result 4: []>

3. DividirDespuésN: Esta función divide una cadena en todas las subcadenas después de cada instancia del separador dado y devuelve un segmento que contiene estas subcadenas.
Sintaxis:

func SplitAfterN(str, sep string, m int) []string>

Aquí, cadena es la cuerda, sep es el separador y m se usa para encontrar el número de subcadenas a devolver. Aquí, si m>0 , luego regresa como máximo metro subcadenas y la última subcadena de cadena no se dividirán. Si metro == 0 , entonces devolverá nulo. Si m<0 , luego devolverá todas las subcadenas.
Ejemplo:

Ir




// Go program to illustrate how to split a string> package> main> import> (> >'fmt'> >'strings'> )> // Main function> func> main() {> >// Creating and initializing the strings> >str1 :=>'Welcome, to the, online portal, of techcodeview.com'> >str2 :=>'My dog name is Dollar'> >str3 :=>'I like to play Ludo'> >// Displaying strings> >fmt.Println(>'String 1: '>, str1)> >fmt.Println(>'String 2: '>, str2)> >fmt.Println(>'String 3: '>, str3)> >// Splitting the given strings> >// Using SplitAfterN() function> >res1 := strings.SplitAfterN(str1,>','>,>2>)> >res2 := strings.SplitAfterN(str2,>''>,>4>)> >res3 := strings.SplitAfterN(str3,>'!'>,>1>)> >res4 := strings.SplitAfterN(>''>,>'techcodeview.com, geeks'>,>3>)> >// Displaying the result> >fmt.Println(>' Result 1: '>, res1)> >fmt.Println(>'Result 2: '>, res2)> >fmt.Println(>'Result 3: '>, res3)> >fmt.Println(>'Result 4: '>, res4)> }>

>

>

Producción:

String 1: Welcome, to the, online portal, of techcodeview.com String 2: My dog name is Dollar String 3: I like to play Ludo  Result 1: [Welcome, to the, online portal, of techcodeview.com] Result 2: [M y dog name is Dollar] Result 3: [I like to play Ludo] Result 4: []>