【JKSJ49-9】字符串

107 阅读1分钟

string和其他主要编程语言的差异

1.string是数据类型,不是引用或指针类型
2.string是只读的byte slice,len函数是它所包含的byte3.stringbyte数组可以存放任何数据

unicode vs utf8

1.unicode是一种字符集(code point)
2.utf8是unicode的存储实现(转换为字节序列的规则)

代码如下所示:

func TestString(t *testing.T) {
   var s string
   t.Log(s)
   s = "hello"
   t.Log(len(s))
   // s[1] = '3' //string是不可变的byte slice
   s = "\xE4\xB8\xA5" // 可以存储任何二进制数据
   t.Log(s)
   t.Log(len(s))
   s = "中"
   t.Log(len(s))
   c := []rune(s)
   t.Log(len(c))
   t.Logf("中 unicode %x", c[0])
   t.Logf("中 utf8 %x", s)
}

程序返回:

=== RUN   TestString
    string_test.go:11: 
    string_test.go:13: 5
    string_test.go:16: 
    string_test.go:17: 3
    string_test.go:19: 3
    string_test.go:21: 1
    string_test.go:22:  unicode 4e2d
    string_test.go:23:  utf8 e4b8ad
--- PASS: TestString (0.00s)
PASS
数据项数据
字符
unicode0x4e2d
utf80xe4b8ad
string/[]byte[0xe4,0xb8,0xad]

字符和对应的字节编码

代码如下所示:

func TestStringToRune(t *testing.T) {
   s := "我是个好人"
   for _, c := range s {
      t.Logf("%[1]c %[1]x", c)
   }
}

程序返回:

=== RUN   TestStringToRune
    string_test.go:29:  6211
    string_test.go:29:  662f
    string_test.go:29:  4e2a
    string_test.go:29:  597d
    string_test.go:29:  4eba
--- PASS: TestStringToRune (0.00s)

常用的字符串函数

1.strings.Split
2.strings.Join
3.字符串转数字strconv.Atoi和数字转字符串strconv.Itoa

代码1如下所示:

func TestStringFn(t *testing.T) {
   s := "A,B,C"
   parts := strings.Split(s, ",")
   for _, part := range parts {
      t.Log(part)
   }
   t.Log(strings.Join(parts, "-"))
}

程序返回:

=== RUN   TestStringFn
    string_test.go:37: A
    string_test.go:37: B
    string_test.go:37: C
    string_test.go:39: A-B-C
--- PASS: TestStringFn (0.00s)
PASS

代码2如下所示:

func TestConv(t *testing.T) {
   s := strconv.Itoa(10)
   t.Log("str" + s)
   if i, err := strconv.Atoi("10"); err == nil {
      t.Log(10 + i)
   }
}

程序返回:

=== RUN   TestConv
    string_test.go:44: str10
    string_test.go:46: 20
--- PASS: TestConv (0.00s)
PASS