Advertisement
PavelZhdanov

Untitled

Feb 6th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.46 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "unicode/utf8"
  6. )
  7.  
  8. func main() {
  9.     s := "Hello, 龙"
  10.  
  11.     fmt.Println(len(s))
  12.     fmt.Println(utf8.RuneCountInString(s))
  13.  
  14.     for i := 0; i < len(s); {
  15.         r, size := utf8.DecodeRuneInString(s[i:])
  16.         fmt.Printf("%d\t%c\n", i, r)
  17.         i += size
  18.     }
  19.  
  20.     //Traversing by string
  21.     for i, r := range s {
  22.         fmt.Printf("%d\t%q\t%d\n", i, r, r)
  23.     }
  24.  
  25.     //Counting of amount runes
  26.     n := 0
  27.     for range s {
  28.         n++
  29.     }
  30.     fmt.Printf("n=%d\n", n)
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement