A string
is not a single rune
, it may contain multiple runes
. You may use a simple type conversion to convert a string
to a []runes
containing all its runes like []rune(sample)
.
The for range
iterates over the runes of a string
, so in your example runeValue
is of type rune
, you may use it in your converter
map, e.g.:
var converter = map[rune]rune{}
sample := "⌘こんにちは"
for _, runeValue := range sample {
converter[runeValue] = runeValue
}
fmt.Println(converter)
But since rune
is an alias for int32
, printing the above converter
map will print integer numbers, output will be:
map[8984:8984 12371:12371 12385:12385 12395:12395 12399:12399 12435:12435]
If you want to print characters, use the %c
verb of fmt.Printf()
:
fmt.Printf("%c\n", converter)
Which will output:
map[⌘:⌘ こ:こ ち:ち に:に は:は ん:ん]
Try the examples on the Go Playground.
If you want to replace (switch) certain runes in a string
, use the strings.Map()
function, for example:
sample := "⌘こんにちは"
result := strings.Map(func(r rune) rune {
if r == '⌘' {
return 'a'
}
if r == 'こ' {
return 'b'
}
return r
}, sample)
fmt.Println(result)
Which outputs (try it on the Go Playground):
abんにちは
If you want the replacements defined by a converter
map:
var converter = map[rune]rune{
'⌘': 'a',
'こ': 'b',
}
sample := "⌘こんにちは"
result := strings.Map(func(r rune) rune {
if c, ok := converter[r]; ok {
return c
}
return r
}, sample)
fmt.Println(result)
This outputs the same. Try this one on the Go Playground.
[]rune(sample)
? like in play.golang.org/p/ISPOEvSzFQW – Sherlynshermstring
is not arune
, it may contain multiplerune
s. Thefor range
iterates over its runes, or you may use a conversion like[]rune(sample)
to get all runes of astring
as a slice. What is your question exactly? – Curarerange
over a string, I want to take eachrune
and use it in my map to find the correspondingrune
. btw, I am trying to implement a character encoding – Massasauga"%+q"
to take each rune butSprintf
returns as a string. I was wondering if there is another way which I can use"%+q"
and returns as a rune – Massasauga