Created
April 1, 2017 03:18
-
-
Save ifukazoo/8aa5a4777f90b3d782146923bb07c3ee to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package stringmisc | |
import ( | |
"fmt" | |
"unicode/utf8" | |
) | |
func stringリテラル() { | |
// ユニコードポイント | |
// 世 :Unicode 0x4e16 | |
// 界 :Unicode 0x754c | |
var s string | |
// UTF-8のエンコーディング表現で. | |
s = "\xe4\xb8\x96\xe7\x95\x8c" | |
fmt.Println(s) | |
// ユニコードエスケープ形式で作る. | |
s = "\u4e16\u754c" | |
fmt.Println(s) | |
s = "\U00004e16\U0000754c" | |
fmt.Println(s) | |
} | |
func string変換() { | |
// string -> []byte(UTF-8でエンコードされたbyte列) | |
bytes := []byte("世界") // => []byte{0xe4,0xb8,0x96,0xe7,0x95,0x8c} | |
fmt.Printf("% x\n", bytes) | |
// string -> rune(ユニコードコードポイント配列) | |
runes := []rune("世界") // => []rune{0x4e16, 0x754c} | |
fmt.Printf("%x\n", runes) | |
var s string | |
// []byte -> string | |
s = string([]byte{0xe4, 0xb8, 0x96, 0xe7, 0x95, 0x8c}) | |
fmt.Println(s) | |
// []rune -> string | |
s = string([]rune{'\u4e16', 0x754c}) | |
fmt.Println(s) | |
} | |
func string文字単位のアクセス() { | |
s := "こんにちは, world" | |
// byteアクセス | |
var length int | |
length = len(s) // 22 | |
fmt.Printf("%d\n", length) | |
for i := 0; i < len(s); i++ { | |
fmt.Printf(" %x", s[i]) | |
} | |
fmt.Println("") | |
// runeアクセス | |
length = utf8.RuneCountInString(s) // 12 | |
fmt.Printf("%d\n", length) | |
for _, r := range s { | |
fmt.Printf("%q", r) | |
} | |
fmt.Println("") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment