Strings and Runes
This Go program demonstrates how to work with Unicode characters in a string.
It includes functionality to print the length of a string in bytes, iterate over each byte to print its hexadecimal representation, count the number of runes in the string, and iterate over each rune to print its Unicode code point and starting index.
Additionally, it uses the utf8.DecodeRuneInString
function to decode runes and provides an example function, examineRune
, to examine specific runes.
Output
Explanation:
The program initializes a constant string
s
with the value "สวัสดี", which means "hello" in Thai.The length of the string in bytes is printed using
len(s)
.A loop iterates over each byte of the string, printing its hexadecimal representation.
The
utf8.RuneCountInString
function is used to count the number of runes in the string.A loop using
range
iterates over each rune in the string, printing its Unicode code point and starting index.Another loop uses
utf8.DecodeRuneInString
to iterate over each rune and print its Unicode code point and starting index.The
examineRune
function is called to examine specific runes and print custom messages based on their values.
Last updated