06_字符串操作.go
package main
import (
"fmt"
"strings"
)
func main() {
//"hellogo"中是否包含"hello", 包含返回true, 不包含返回false
fmt.Println(strings.Contains("hellogo", "hello"))
fmt.Println(strings.Contains("hellogo", "abc"))
//Joins 组合
s := []string{"abc", "hello", "mike", "go"}
buf := strings.Join(s, "x")
fmt.Println("buf = ", buf)
//Index, 查找子串的位置
fmt.Println(strings.Index("abcdhello", "hello"))
fmt.Println(strings.Index("abcdhello", "go")) //不包含子串返回-1
buf = strings.Repeat("go", 3)
fmt.Println("buf = ", buf) //"gogogo"
//Split 以指定的分隔符拆分
buf = "hello@abc@go@mike"
s2 := strings.Split(buf, "@")
fmt.Println("s2 = ", s2)
//Trim去掉两头的字符
buf = strings.Trim(" are u ok? ", " ") //去掉2头空格
fmt.Printf("buf = #%s#\n", buf)
//去掉空格,把元素放入切片中
s3 := strings.Fields(" are u ok? ")
//fmt.Println("s3 = ", s3)
for i, data := range s3 {
fmt.Println(i, ", ", data)
}
}
07_字符串转换.go
package main
import (
"fmt"
"strconv"
)
func main() {
//转换为字符串后追加到字节数组
slice := make([]byte, 0, 1024)
slice = strconv.AppendBool(slice, true)
//第二个数为要追加的数,第3个为指定10进制方式追加
slice = strconv.AppendInt(slice, 1234, 10)
slice = strconv.AppendQuote(slice, "abcgohello")
fmt.Println("slice = ", string(slice)) //转换string后再打印
//其它类型转换为字符串
var str string
str = strconv.FormatBool(false)
//'f' 指打印格式,以小数方式, -1指小数点位数(紧缩模式), 64以float64处理
str = strconv.FormatFloat(3.14, 'f', -1, 64)
//整型转字符串,常用
str = strconv.Itoa(6666)
fmt.Println("str = ", str)
//字符串转其它类型
var flag bool
var err error
flag, err = strconv.ParseBool("true")
if err == nil {
fmt.Println("flag = ", flag)
} else {
fmt.Println("err = ", err)
}
//把字符串转换为整型
a, _ := strconv.Atoi("567")
fmt.Println("a = ", a)
}
08_正则表达式1.go
package main
import (
"fmt"
"regexp"
)
func main() {
buf := "abc azc a7c aac 888 a9c tac"
//1) 解释规则, 它会解析正则表达式,如果成功返回解释器
//reg1 := regexp.MustCompile(`a.c`)
//reg1 := regexp.MustCompile(`a[0-9]c`)
reg1 := regexp.MustCompile(`a\dc`)
if reg1 == nil { //解释失败,返回nil
fmt.Println("regexp err")
return
}
//2) 根据规则提取关键信息
result1 := reg1.FindAllStringSubmatch(buf, -1)
fmt.Println("result1 = ", result1)
}
09_正则表达式2.go
package main
import (
"fmt"
"regexp"
)
func main() {
buf := "43.14 567 agsdg 1.23 7. 8.9 1sdljgl 6.66 7.8 "
//解释正则表达式, +匹配前一个字符的1次或多次
reg := regexp.MustCompile(`\d+\.\d+`)
if reg == nil {
fmt.Println("MustCompile err")
return
}
//提取关键信息
//result := reg.FindAllString(buf, -1)
result := reg.FindAllStringSubmatch(buf, -1)
fmt.Println("result = ", result)
}
10_正则表达式3.go
package main
import (
"fmt"
"regexp"
)
func main() {
//`` 原生字符串
buf := `
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<title>Go语言标准库文档中文版 | Go语言中文网 | Golang中文社区 | Golang中国</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
<meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
<meta charset="utf-8">
<link rel="shortcut icon" href="/static/img/go.ico">
<link rel="apple-touch-icon" type="image/png" href="/static/img/logo2.png">
<meta name="author" content="polaris <polaris@studygolang.com>">
<meta name="keywords" content="中文, 文档, 标准库, Go语言,Golang,Go社区,Go中文社区,Golang中文社区,Go语言社区,Go语言学习,学习Go语言,Go语言学习园地,Golang 中国,Golang中国,Golang China, Go语言论坛, Go语言中文网">
<meta name="description" content="Go语言文档中文版,Go语言中文网,中国 Golang 社区,Go语言学习园地,致力于构建完善的 Golang 中文社区,Go语言爱好者的学习家园。分享 Go 语言知识,交流使用经验">
</head>
<div>和爱好</div>
<div>哈哈
你在吗
不在
</div>
<div>测试</div>
<div>你过来啊</div>
<frameset cols="15,85">
<frame src="/static/pkgdoc/i.html">
<frame name="main" src="/static/pkgdoc/main.html" tppabs="main.html" >
<noframes>
</noframes>
</frameset>
</html>
`
//解释正则表达式, +匹配前一个字符的1次或多次
//reg := regexp.MustCompile(`<div>(.*)</div>`)
reg := regexp.MustCompile(`<div>(?s:(.*?))</div>`)
if reg == nil {
fmt.Println("MustCompile err")
return
}
//提取关键信息
result := reg.FindAllStringSubmatch(buf, -1)
//fmt.Println("result = ", result)
//过滤<></>
for _, text := range result {
//fmt.Println("text[0] = ", text[0]) //带<></>
fmt.Println("text[1] = ", text[1]) //不带<></>
}
}