为编程爱好者分享易语言教程源码的资源网
好用的代理IP,游戏必备 ____广告位招租____ 服务器99/年 ____广告位招租____ ____广告位招租____ 挂机,建站服务器
好用的代理IP,游戏必备 ____广告位招租____ 服务器低至38/年 ____广告位招租____ ____广告位招租____ 挂机,建站服务器

网站首页 > 网络编程 > 其它综合 正文

ch12、字符串(string)与字符编码

三叶资源网 2023-01-02 20:18:50 其它综合 200 ℃ 0 评论

1、字符串

与其它编程语言的差异:

  • string是数据类型,不是引用或指针类型
  • string是只读的byte slice(不可变类型),len计算的是包含所有byte数
  • string的byte数组可以存放任何数据
package string

import "testing"

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))   // 3
}

2、字符编码:Unicode VS. UTF-8

Unicode 是一种编码符号表示集(code point)

UTF8 是 unicode 的存储实现 (转换为字节序列的规则)方式之一

注意: Unicode只是一个符号集,它只规定了符号的二进制代码,却没有规定这个二进制代码应该如何存储。(是按照两个字节还是三个字节or四个字节存储?)

package string

import "testing"

func TestString(t *testing.T) {
	var s string
	s = "中"
	t.Log(len(s))

	c := []rune(s)
	t.Logf("中 unicode:%x", c[0]) // 中 unicode:4e2d
	t.Logf("中 utf-8: %x", s)     // 中 utf-8: e4b8ad
}

对应的表示法:

字符

‘中’

Unicode

0x4E2D

UTF8

0xE4B8AD

String/[]byte

[0xE4,0xB8,0xAD]

可参考资料:

字符编码笔记:ASCII,Unicode 和 UTF-8:http://www.ruanyifeng.com/blog/2007/10/ascii_unicode_and_utf-8.html?from=timeline

Unicode官方符号对应表:https://home.unicode.org/

Unicode汉字对应表:http://www.chi2ko.com/tool/CJK.htm

  • 在UTF-8中一个汉字占3个字节
package main

import "fmt"

func main() {
	str1 := "lnj"
	fmt.Println(len(str1)) // 3
	str2 := "公号:代码情缘"
	fmt.Println(len(str2)) // 21
}

3、常用字符串函数

strings包:https://pkg.go.dev/strings

strconv包:https://pkg.go.dev/strconv

package string_fn

import (
	"strconv"
	"strings"
	"testing"
)

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

func TestIntConv(t *testing.T) {
	s := strconv.Itoa(10)
	t.Log("str" + s)
	if i, err := strconv.Atoi("10"); err == nil { //字符串转换为整型时,需要进行校验是否转换成功
		t.Log(10 + i)
	}
}

4、字符串相关方法

如果字符串中包含中文, 又想精确的计算字符串中字符的个数而不是占用的字节, 那么必须先将字符串转换为rune类型数组

  • Go语言中byte用于保存字符, rune用于保存汉字
package main

import "fmt"

func main() {
	str := "测试:代码之旅"
	// 注意byte占1个字节, 只能保存字符不能保存汉字,因为一个汉字占用3个字节
	arr1 := []byte(str) // 21
	fmt.Println(len(arr1))
	for _, v := range arr1 {
		fmt.Printf("%c", v) // ?
???·?????£? ???
???
	}
	fmt.Println()

	// Go语言中rune类型就是专门用于保存汉字的
	arr2 := []rune(str)
	fmt.Println(len(arr2)) // 7
	for _, v := range arr2 {
		fmt.Printf("%c", v) // 测试:代码之旅
	}
}

查找子串在字符串中出现的位置

    • func Index(s, sep string) int
    • func IndexByte(s string, c byte) int
    • func IndexRune(s string, r rune) int
    • func IndexAny(s, chars string) int
    • func IndexFunc(s string, f func(rune) bool) int
    • func LastIndex(s, sep string) int
    • func LastIndexByte(s string, c byte) int
    • func LastIndexAny(s, chars string) int
    • func LastIndexFunc(s string, f func(rune) bool) int
package main

import (
	"fmt"
	"strings"
)

func main() {
	// 查找`字符`在字符串中第一次出现的位置, 找不到返回-1
	res := strings.IndexByte("hello 李南江", 'l')
	fmt.Println(res) // 2

	// 查找`汉字`OR`字符`在字符串中第一次出现的位置, 找不到返回-1
	res = strings.IndexRune("hello 李南江", '李')
	fmt.Println(res) // 6
	res = strings.IndexRune("hello 李南江", 'l')
	fmt.Println(res) // 2

	// 查找`汉字`OR`字符`中任意一个在字符串中第一次出现的位置, 找不到返回-1
	res = strings.IndexAny("hello 李南江", "wml")
	fmt.Println(res) // 2
	// 会把wmhl拆开逐个查找, w、m、h、l只要任意一个被找到, 立刻停止查找
	res = strings.IndexAny("hello 李南江", "wmhl")
	fmt.Println(res) // 0
	// 查找`子串`在字符串第一次出现的位置, 找不到返回-1
	res = strings.Index("hello 李南江", "llo")
	fmt.Println(res) // 2
	// 会把lle当做一个整体去查找, 而不是拆开
	res = strings.Index("hello 李南江", "lle")
	fmt.Println(res) // -1
	// 可以查找字符也可以查找汉字
	res = strings.Index("hello 李南江", "李")
	fmt.Println(res) // 6

	// 会将字符串先转换为[]rune, 然后遍历rune切片逐个取出传给自定义函数
	// 只要函数返回true,代表符合我们的需求, 既立即停止查找
	res = strings.IndexFunc("hello 李南江", custom)
	fmt.Println(res) // 4

	// 倒序查找`子串`在字符串第一次出现的位置, 找不到返回-1
	res = strings.LastIndex("hello 李南江", "l")
	fmt.Println(res) // 3
}
func custom(r rune) bool {
	fmt.Printf("被调用了, 当前传入的是%c\n", r)
	if r == 'o' {
		return true
	}
	return false
}

判断字符串中是否包含子串

    • func Contains(s, substr string) bool
    • func ContainsRune(s string, r rune) bool
    • func ContainsAny(s, chars string) bool
    • func HasPrefix(s, prefix string) bool
    • func HasSuffix(s, suffix string) bool
package main

import (
	"fmt"
	"strings"
)

func main() {
	// 查找`子串`在字符串中是否存在, 存在返回true, 不存在返回false
	// 底层实现就是调用strings.Index函数
	res := strings.Contains("hello 代码情缘", "llo")
	fmt.Println(res) // true

	// 查找`汉字`OR`字符`在字符串中是否存在, 存在返回true, 不存在返回false
	// 底层实现就是调用strings.IndexRune函数
	res = strings.ContainsRune("hello 代码情缘", 'l')
	fmt.Println(res) // true
	res = strings.ContainsRune("hello 代码情缘", '李')
	fmt.Println(res) // false

	// 查找`汉字`OR`字符`中任意一个在字符串中是否存在, 存在返回true, 不存在返回false
	// 底层实现就是调用strings.IndexAny函数
	res = strings.ContainsAny("hello 代码情缘", "wmhl")
	fmt.Println(res) // true

	// 判断字符串是否已某个字符串开头
	res = strings.HasPrefix("lnj-book.avi", "lnj")
	fmt.Println(res) // true

	// 判断字符串是否已某个字符串结尾
	res = strings.HasSuffix("lnj-book.avi", ".avi")
	fmt.Println(res) // true
}

字符串比较

    • func Compare(a, b string) int
    • func EqualFold(s, t string) bool
package main

import (
	"fmt"
	"strings"
)

func main() {
	// 比较两个字符串大小, 会逐个字符地进行比较ASCII值
	// 第一个参数 >  第二个参数 返回 1
	// 第一个参数 <  第二个参数 返回 -1
	// 第一个参数 == 第二个参数 返回 0
	res := strings.Compare("bcd", "abc")
	fmt.Println(res) // 1
	res = strings.Compare("bcd", "bdc")
	fmt.Println(res) // -1
	res = strings.Compare("bcd", "bcd")
	fmt.Println(res) // 0

	// 判断两个字符串是否相等, 可以判断字符和中文
	// 判断时会忽略大小写进行判断
	res2 := strings.EqualFold("abc", "def")
	fmt.Println(res2) // false
	res2 = strings.EqualFold("abc", "abc")
	fmt.Println(res2) // true
	res2 = strings.EqualFold("abc", "ABC")
	fmt.Println(res2) // true
	res2 = strings.EqualFold("代码情缘", "代码情缘")
	fmt.Println(res2) // true
}

字符串转换

  • func ToUpper(s string) string
  • func ToLower(s string) string
  • func ToTitle(s string) string
  • func ToUpperSpecial(_case unicode.SpecialCase, s string) string
  • func ToLowerSpecial(_case unicode.SpecialCase, s string) string
  • func ToTitleSpecial(_case unicode.SpecialCase, s string) string
  • func Title(s string) string
package main

import (
	"fmt"
	"strings"
)

func main() {
	// 将字符串转换为小写
	res := strings.ToLower("ABC")
	fmt.Println(res) // abc

	// 将字符串转换为大写
	res = strings.ToUpper("abc")
	fmt.Println(res) // ABC

	// 将字符串转换为标题格式, 大部分`字符`标题格式就是大写
	res = strings.ToTitle("hello world")
	fmt.Println(res) // HELLO WORLD
	res = strings.ToTitle("HELLO WORLD")
	fmt.Println(res) // HELLO WORLD

	// 将单词首字母变为大写, 其它字符不变
	// 单词之间用空格OR特殊字符隔开
	res = strings.Title("hello world")
	fmt.Println(res) // Hello World
}

字符串拆合

  • func Split(s, sep string) []string
  • func SplitN(s, sep string, n int) []string
  • func SplitAfter(s, sep string) []string
  • func SplitAfterN(s, sep string, n int) []string
  • func Fields(s string) []string
  • func FieldsFunc(s string, f func(rune) bool) []string
  • func Join(a []string, sep string) string
  • func Repeat(s string, count int) string
  • func Replace(s, old, new string, n int) string
package main

import (
	"fmt"
	"strings"
)

func main() {
	// 按照指定字符串切割原字符串
	// 用,切割字符串
	arr1 := strings.Split("a,b,c", ",")
	fmt.Println(arr1, " ", len(arr1)) // [a b c] 3
	arr2 := strings.Split("ambmc", "m")
	fmt.Println(arr2, " ", len(arr2)) // [a b c] 3

	// 按照指定字符串切割原字符串, 并且指定切割为几份
	// 如果最后一个参数为0, 那么会范围一个空数组
	arr3 := strings.SplitN("a,b,c", ",", 2)
	fmt.Println(arr3, " ", len(arr3)) // [a b,c] 2
	arr31 := strings.SplitN("a,b,c", ",", 1)
	fmt.Println(arr31, " ", len(arr31)) // [a,b,c] 1
	arr4 := strings.SplitN("a,b,c", ",", 0)
	fmt.Println(arr4, " ", len(arr4)) // [] 0

	// 按照指定字符串切割原字符串, 切割时包含指定字符串
	arr5 := strings.SplitAfter("a,b,c", ",")
	fmt.Println(arr5, " ", len(arr5)) // [a, b, c] 3

	// 按照指定字符串切割原字符串, 切割时包含指定字符串, 并且指定切割为几份
	arr6 := strings.SplitAfterN("a,b,c", ",", 2)
	fmt.Println(arr6, " ", len(arr6)) // [a, b,c] 2

	// 按照空格切割字符串, 多个空格会合并为一个空格处理
	arr7 := strings.Fields("a  b c    d")
	fmt.Println(arr7) // [a b c d]

	// 将字符串转换成切片传递给函数之后由函数决定如何切割
	// 类似于IndexFunc
	arr8 := strings.FieldsFunc("a,b,c", custom)
	fmt.Println(arr8) // [a b c]

	// 将字符串切片按照指定连接符号转换为字符串
	sce := []string{"aa", "bb", "cc"}
	fmt.Println(sce)
	str1 := strings.Join(sce, "-")
	fmt.Println(str1) // aa-bb-cc

	// 返回count个s串联的指定字符串
	str2 := strings.Repeat("abc", 2)
	fmt.Println(str2) // abcabc

	// 第一个参数: 需要替换的字符串
	// 第二个参数: 旧字符串
	// 第三个参数: 新字符串
	// 第四个参数: 用新字符串 替换 多少个旧字符串
	// 注意点: 传入-1代表只要有旧字符串就替换
	// 注意点: 替换之后会生成新字符串, 原字符串不会受到影响
	str3 := "abcdefabcdefabc"
	str4 := strings.Replace(str3, "abc", "mmm", -1)
	fmt.Println(str3) // abcdefabcdefabc
	fmt.Println(str4) // mmmdefmmmdefmmm
}
func custom(r rune) bool {
	fmt.Printf("被调用了, 当前传入的是%c\n", r)
	if r == ',' {
		return true
	}
	return false
}

字符串清理

  • func Trim(s string, cutset string) string
  • func TrimLeft(s string, cutset string) string
  • func TrimRight(s string, cutset string) string
  • func TrimFunc(s string, f func(rune) bool) string
  • func TrimLeftFunc(s string, f func(rune) bool) string
  • func TrimRightFunc(s string, f func(rune) bool) string
  • func TrimSpace(s string) string
  • func TrimPrefix(s, prefix string) string
  • func TrimSuffix(s, suffix string) string
package main

import (
	"fmt"
	"strings"
)

func main() {
	// 去除字符串两端指定字符
	str1 := strings.Trim("!!!abc!!!def!!!", "!")
	fmt.Println(str1) // abc!!!def
	// 去除字符串左端指定字符
	str2 := strings.TrimLeft("!!!abc!!!def!!!", "!")
	fmt.Println(str2) // abc!!!def!!!
	// 去除字符串右端指定字符
	str3 := strings.TrimRight("!!!abc!!!def!!!", "!")
	fmt.Println(str3) // !!!abc!!!def
	// // 去除字符串两端空格
	str4 := strings.TrimSpace("   abc!!!def ")
	fmt.Println(str4) // abc!!!def

	// 按照方法定义规则,去除字符串两端符合规则内容
	str5 := strings.TrimFunc("!!!abc!!!def!!!", custom)
	fmt.Println(str5) // abc!!!def
	// 按照方法定义规则,去除字符串左端符合规则内容
	str6 := strings.TrimLeftFunc("!!!abc!!!def!!!", custom)
	fmt.Println(str6) // abc!!!def!!!
	//  按照方法定义规则,去除字符串右端符合规则内容
	str7 := strings.TrimRightFunc("!!!abc!!!def!!!", custom)
	fmt.Println(str7) // !!!abc!!!def

	// 取出字符串开头的指定字符串
	str8 := strings.TrimPrefix("lnj-book.avi", "lnj-")
	fmt.Println(str8) // book.avi

	// 取出字符串结尾的指定字符串
	str9 := strings.TrimSuffix("lnj-book.avi", ".avi")
	fmt.Println(str9) // lnj-book
}

func custom(r rune) bool {
	fmt.Printf("被调用了, 当前传入的是%c\n", r)
	if r == '!' {
		return true
	}
	return false
}

来源:三叶资源网,欢迎分享,公众号:iisanye,(三叶资源网⑤群:21414575

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

百度站内搜索
关注微信公众号
三叶资源网⑤群:三叶资源网⑤群

网站分类
随机tag
httpwacthHttpWatch远程文件管理微信封包组包POST登录TGP饥荒一键指令工具图像处理模块AES高效加解密POSTASP网络验证命令行卸载node获取正文算法读卡器开发DLL劫持源码时时彩二星缩水软件百度识图源码支持库源码易语言例程动态调用汇编
最新评论