Skip to content

Commit c0d3cff

Browse files
committed
字符串
Signed-off-by: Tinywan <756684177@qq.com>
1 parent 741d9b9 commit c0d3cff

File tree

6 files changed

+729
-279
lines changed

6 files changed

+729
-279
lines changed

demo/interface/demo01.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package main
2+
3+
// interface define 元音
4+
type VowelsFinder interface {
5+
FindVowels() []rune
6+
}
7+
8+
func main() {
9+
10+
}

demo/string/byte.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func main() {
8+
byteSlice := []byte{0x43, 0x61, 0x66, 0xC3, 0xA9}
9+
str := string(byteSlice)
10+
fmt.Println(str)
11+
12+
byteSlice2 := []byte{67, 97, 102, 195, 169} //decimal equivalent of {'\x43', '\x61', '\x66', '\xC3', '\xA9'}
13+
str2 := string(byteSlice2)
14+
fmt.Println(str2)
15+
16+
runeSlice := []rune{0x0053, 0x0065, 0x00f1, 0x006f, 0x0072}
17+
str3 := string(runeSlice)
18+
fmt.Println(str3)
19+
}

demo/string/length.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"unicode/utf8"
6+
)
7+
8+
func length(s string) {
9+
fmt.Printf("length of %s is %d\n", s, utf8.RuneCountInString(s))
10+
}
11+
12+
func main() {
13+
word1 := "Señor"
14+
length(word1)
15+
word2 := "Pets"
16+
length(word2)
17+
}

demo/string/rune.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
// 打印字符串中的字节
8+
func printBytes(s string) {
9+
for i := 0; i < len(s); i++ {
10+
fmt.Printf("%x ", s[i])
11+
}
12+
}
13+
14+
// 打印字符串中的字符
15+
func printChars(s string) {
16+
runes := []rune(s)
17+
for i := 0; i < len(runes); i++ {
18+
fmt.Printf("%c", runes[i])
19+
}
20+
}
21+
22+
func main() {
23+
name := "Hello World"
24+
printBytes(name)
25+
fmt.Printf("\n")
26+
printChars(name)
27+
28+
fmt.Printf("\n")
29+
30+
// 特殊字符
31+
name = "Señor"
32+
printBytes(name)
33+
fmt.Printf("\n")
34+
printChars(name)
35+
36+
// 打印汉字
37+
fmt.Printf("\n")
38+
name = "萬少波"
39+
printBytes(name)
40+
fmt.Printf("\n")
41+
printChars(name)
42+
}

0 commit comments

Comments
 (0)