golang substring rune + special character

Identificador: 11551
Autor: Ricardo Bocchi   segunda-feira, Dia 1 de maio de 2023, às 02:53:31 horas
Revisado Por:    segunda-feira, Dia 13 de fevereiro de 2017, às 23:19:21 horas
Visualização: Todos

Pontos: 0   Acessos: 5218



Usando a função rune podemos pegar o substring de uma string se sem preocupar com caracteres especiais:

Por exemplo

str := "Verificar o nível de combustível do reservatório e, se necessário, complementar"

r := []rune(str)

fmt.Println(len(str)) // 83

fmt.Println(len(r)) // 79

fmt.Println(str[0:14]) // Verificar o n�

fmt.Println(string(r[0:14])) // Verificar o ní