count_characters.go 697 B

12345678910111213141516171819202122
  1. package main
  2. import (
  3. "fmt"
  4. "unicode/utf8"
  5. )
  6. func main() {
  7. // count number of characters:
  8. str1 := "asSASA ddd dsjkdsjs dk"
  9. fmt.Printf("The number of bytes in string str1 is %d\n",len(str1))
  10. fmt.Printf("The number of characters in string str1 is %d\n",utf8.RuneCountInString(str1))
  11. str2 := "asSASA ddd dsjkdsjsこん dk"
  12. fmt.Printf("The number of bytes in string str2 is %d\n",len(str2))
  13. fmt.Printf("The number of characters in string str2 is %d",utf8.RuneCountInString(str2))
  14. }
  15. /* Output:
  16. The number of bytes in string str1 is 22
  17. The number of characters in string str1 is 22
  18. The number of bytes in string str2 is 28
  19. The number of characters in string str2 is 24
  20. */