字符串是不可變值類型,內部用指針指向 UTF-8 字節數組。
~~~
? 默認值是空字符串 ""。
? 用索引號訪問某字節,如 s[i]。
? 不能用序號獲取字節元素指針,&s[i] 非法。
? 不可變類型,無法修改字節數組。
? 字節數組尾部不包含 NULL。
~~~
使用索引號訪問字符 (byte)。
~~~
package main
func main() {
s := "abc"
println(s[0] == '\x61', s[1] == 'b', s[2] == 0x63)
}
~~~
輸出結果:
~~~
true true true
~~~
使用 " ` " 定義不做轉義處理的原始字符串,支持跨行。
~~~
package main
func main() {
s := `a
b\r\n\x00
c`
println(s)
}
~~~
輸出結果:
~~~
a
b\r\n\x00
c
~~~
連接跨行字符串時,"+" 必須在上一行末尾,否則導致編譯錯誤。
~~~
package main
import (
"fmt"
)
func main() {
s := "Hello, " +
"World!"
// s2 := "Hello, "
// +"World!"
//./main.go:11:2: invalid operation: + untyped string
fmt.Println(s)
}
~~~
支持用兩個索引號 ([]) 返回子串。 串依然指向原字節數組,僅修改了指針和 度屬性。
~~~
package main
import (
"fmt"
)
func main() {
s := "Hello, World!"
s1 := s[:5] // Hello
s2 := s[7:] // World!
s3 := s[1:5] // ello
fmt.Println(s, s1, s2, s3)
}
~~~
輸出結果:
~~~
Hello, World! Hello World! ello
~~~
單引號字符常量表示 Unicode Code Point, 持 \uFFFF、\U7FFFFFFF、\xFF 格式。
對應 rune 類型,UCS-4。
~~~
package main
import (
"fmt"
)
func main() {
fmt.Printf("%T\n", 'a')
var c1, c2 rune = '\u6211', '們'
println(c1 == '我', string(c2) == "\xe4\xbb\xac")
}
~~~
輸出結果:
~~~
int32 // rune 是 int32 的別名
true true
~~~
要修改字符串,可先將其轉換成 []rune 或 []byte,完成后再轉換為 string。無論哪種轉換,都會重新分配內存,并復制字節數組。
~~~
package main
func main() {
s := "abcd"
bs := []byte(s)
bs[1] = 'B'
println(string(bs))
u := "電腦"
us := []rune(u)
us[1] = '話'
println(string(us))
}
~~~
輸出結果:
~~~
aBcd
電話
~~~
for 循環遍歷字符串時,也有 byte 和 rune 兩種方式。
~~~
package main
import (
"fmt"
)
func main() {
s := "abc漢字"
for i := 0; i < len(s); i++ { // byte
fmt.Printf("%c,", s[i])
}
fmt.Println()
for _, r := range s { // rune
fmt.Printf("%c,", r)
}
fmt.Println()
}
~~~
輸出結果:
~~~
a,b,c,?,±,?,?,-,?,
a,b,c,漢,字,
~~~
string的底層布局

字符串處理:
判斷是不是以某個字符串開頭
~~~
package main
import (
"fmt"
"strings"
)
func main() {
str := "hello world"
res0 := strings.HasPrefix(str, "http://")
res1 := strings.HasPrefix(str, "hello")
fmt.Printf("res0 is %v\n", res0)
fmt.Printf("res1 is %v\n", res1)
}
~~~
輸出結果:
~~~
res0 is false
res1 is true
~~~
判斷是不是以某個字符串結尾
~~~
package main
import (
"fmt"
"strings"
)
func main() {
str := "hello world"
res0 := strings.HasSuffix(str, "http://")
res1 := strings.HasSuffix(str, "world")
fmt.Printf("res0 is %v\n", res0)
fmt.Printf("res1 is %v\n", res1)
}
~~~
輸出結果:
~~~
res0 is false
res1 is true
~~~
判斷str在s中首次出現的位置,如果沒有返回-1
~~~
package main
import (
"fmt"
"strings"
)
func main() {
str := "hello world"
res0 := strings.Index(str, "o")
res1 := strings.Index(str, "i")
fmt.Printf("res0 is %v\n", res0)
fmt.Printf("res1 is %v\n", res1)
}
~~~
輸出結果:
~~~
res0 is 4
res1 is -1
~~~
判斷str在s中最后一次出現的位置,如果沒有返回-1
~~~
package main
import (
"fmt"
"strings"
)
func main() {
str := "hello world"
res0 := strings.LastIndex(str, "o")
res1 := strings.LastIndex(str, "i")
fmt.Printf("res0 is %v\n", res0)
fmt.Printf("res1 is %v\n", res1)
}
~~~
輸出結果:
~~~
res0 is 7
res1 is -1
~~~
字符串替換
~~~
package main
import (
"fmt"
"strings"
)
func main() {
str := "hello world world"
res0 := strings.Replace(str, "world", "golang", 2)
res1 := strings.Replace(str, "world", "golang", 1)
//trings.Replace("原字符串", "被替換的內容", "替換的內容", 替換次數)
fmt.Printf("res0 is %v\n", res0)
fmt.Printf("res1 is %v\n", res1)
}
~~~
輸出結果:
~~~
res0 is hello golang golang
res1 is hello golang world
~~~
求str含s的次數
~~~
package main
import (
"fmt"
"strings"
)
func main() {
str := "hello world world"
countTime0 := strings.Count(str, "o")
countTime1 := strings.Count(str, "i")
fmt.Printf("countTime0 is %v\n", countTime0)
fmt.Printf("countTime1 is %v\n", countTime1)
}
~~~
輸出結果:
~~~
countTime0 is 3
countTime1 is 0
~~~
重復 n 次 str
~~~
package main
import (
"fmt"
"strings"
)
func main() {
str := "hello world "
res0 := strings.Repeat(str, 0)
res1 := strings.Repeat(str, 1)
res2 := strings.Repeat(str, 2)
// strings.Repeat("原字符串", 重復次數)
fmt.Printf("res0 is %v\n", res0)
fmt.Printf("res1 is %v\n", res1)
fmt.Printf("res2 is %v\n", res2)
}
~~~
輸出結果:
~~~
res0 is
res1 is hello world
res2 is hello world hello world
~~~
str 轉為大寫
~~~
package main
import (
"fmt"
"strings"
)
func main() {
str := "hello world "
res := strings.ToUpper(str)
fmt.Printf("res is %v\n", res)
}
~~~
輸出結果:
~~~
res is HELLO WORLD
~~~
str 轉為小寫
~~~
package main
import (
"fmt"
"strings"
)
func main() {
str := "HELLO WORLD "
res := strings.ToLower(str)
fmt.Printf("res is %v\n", res)
}
~~~
輸出結果:
~~~
res is hello world
~~~
去掉 str 首尾的空格
~~~
package main
import (
"fmt"
"strings"
)
func main() {
str := " hello world "
res := strings.TrimSpace(str)
fmt.Printf("res is %v\n", res)
}
~~~
去掉字符串首尾指定的字符
~~~
package main
import (
"fmt"
"strings"
)
func main() {
str := "hi , hello world , hi"
res := strings.Trim(str, "hi")
fmt.Printf("res is %v\n", res)
}
~~~
輸出結果:
~~~
res is , hello world ,
~~~
去掉字符串首指定的字符
~~~
package main
import (
"fmt"
"strings"
)
func main() {
str := "hi , hello world , hi"
res := strings.TrimLeft(str, "hi")
fmt.Printf("res is %v\n", res)
}
~~~
輸出結果:
~~~
res is , hello world , hi
~~~
去掉字符串尾指定的字符
~~~
package main
import (
"fmt"
"strings"
)
func main() {
str := "hi , hello world , hi"
res := strings.TrimRight(str, "hi")
fmt.Printf("res is %v\n", res)
}
~~~
輸出結果:
~~~
res is hi , hello world ,
~~~
返回str空格分隔的所有子串的slice,
~~~
package main
import (
"fmt"
"strings"
)
func main() {
str := "hello world ,hello golang"
res := strings.Fields(str)
fmt.Printf("res is %v\n", res)
}
~~~
輸出結果:
~~~
res is [hello world ,hello golang]
~~~
返回str 指定字符分隔的所有子串的slice
~~~
package main
import (
"fmt"
"strings"
)
func main() {
str := "hello world ,hello golang"
res := strings.Split(str, "o")
fmt.Printf("res is %v\n", res)
}
~~~
輸出結果:
~~~
res is [hell w rld ,hell g lang]
~~~
用指定字符將 string 類型的 slice 中所有元素鏈接成一個字符串
~~~
package main
import (
"fmt"
"strings"
)
func main() {
str := []string{"hello", "world", "hello", "golang"}
res := strings.Join(str, "++")
fmt.Printf("res is %v\n", res)
/*
num := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}
res1 := strings.Join(num, "++")
// cannot use num (type []int) as type []string in argument to strings.Join
fmt.Println(res1)
*/
}
~~~
輸出結果:
~~~
res is hello++world++hello++golang
~~~
- 序言
- 目錄
- 環境搭建
- Linux搭建golang環境
- Windows搭建golang環境
- Mac搭建golang環境
- Go 環境變量
- 編輯器
- vs code
- Mac 安裝vs code
- Windows 安裝vs code
- vim編輯器
- 介紹
- 1.Go語言的主要特征
- 2.golang內置類型和函數
- 3.init函數和main函數
- 4.包
- 1.工作空間
- 2.源文件
- 3.包結構
- 4.文檔
- 5.編寫 Hello World
- 6.Go語言 “ _ ”(下劃線)
- 7.運算符
- 8.命令
- 類型
- 1.變量
- 2.常量
- 3.基本類型
- 1.基本類型介紹
- 2.字符串String
- 3.數組Array
- 4.類型轉換
- 4.引用類型
- 1.引用類型介紹
- 2.切片Slice
- 3.容器Map
- 4.管道Channel
- 5.指針
- 6.自定義類型Struct
- 流程控制
- 1.條件語句(if)
- 2.條件語句 (switch)
- 3.條件語句 (select)
- 4.循環語句 (for)
- 5.循環語句 (range)
- 6.循環控制Goto、Break、Continue
- 函數
- 1.函數定義
- 2.參數
- 3.返回值
- 4.匿名函數
- 5.閉包、遞歸
- 6.延遲調用 (defer)
- 7.異常處理
- 8.單元測試
- 壓力測試
- 方法
- 1.方法定義
- 2.匿名字段
- 3.方法集
- 4.表達式
- 5.自定義error
- 接口
- 1.接口定義
- 2.執行機制
- 3.接口轉換
- 4.接口技巧
- 面向對象特性
- 并發
- 1.并發介紹
- 2.Goroutine
- 3.Chan
- 4.WaitGroup
- 5.Context
- 應用
- 反射reflection
- 1.獲取基本類型
- 2.獲取結構體
- 3.Elem反射操作基本類型
- 4.反射調用結構體方法
- 5.Elem反射操作結構體
- 6.Elem反射獲取tag
- 7.應用
- json協議
- 1.結構體轉json
- 2.map轉json
- 3.int轉json
- 4.slice轉json
- 5.json反序列化為結構體
- 6.json反序列化為map
- 終端讀取
- 1.鍵盤(控制臺)輸入fmt
- 2.命令行參數os.Args
- 3.命令行參數flag
- 文件操作
- 1.文件創建
- 2.文件寫入
- 3.文件讀取
- 4.文件刪除
- 5.壓縮文件讀寫
- 6.判斷文件或文件夾是否存在
- 7.從一個文件拷貝到另一個文件
- 8.寫入內容到Excel
- 9.日志(log)文件
- server服務
- 1.服務端
- 2.客戶端
- 3.tcp獲取網頁數據
- 4.http初識-瀏覽器訪問服務器
- 5.客戶端訪問服務器
- 6.訪問延遲處理
- 7.form表單提交
- web模板
- 1.渲染終端
- 2.渲染瀏覽器
- 3.渲染存儲文件
- 4.自定義io.Writer渲染
- 5.模板語法
- 時間處理
- 1.格式化
- 2.運行時間
- 3.定時器
- 鎖機制
- 互斥鎖
- 讀寫鎖
- 性能比較
- sync.Map
- 原子操作
- 1.原子增(減)值
- 2.比較并交換
- 3.導入、導出、交換
- 加密解密
- 1.md5
- 2.base64
- 3.sha
- 4.hmac
- 常用算法
- 1.冒泡排序
- 2.選擇排序
- 3.快速排序
- 4.插入排序
- 5.睡眠排序
- 限流器
- 日志包
- 日志框架logrus
- 隨機數驗證碼
- 生成指定位數的隨機數
- 生成圖形驗證碼
- 編碼格式轉換
- UTF-8與GBK
- 解決中文亂碼
- 設計模式
- 創建型模式
- 單例模式
- singleton.go
- singleton_test.go
- 抽象工廠模式
- abstractfactory.go
- abstractfactory_test.go
- 工廠方法模式
- factorymethod.go
- factorymethod_test.go
- 原型模式
- prototype.go
- prototype_test.go
- 生成器模式
- builder.go
- builder_test.go
- 結構型模式
- 適配器模式
- adapter.go
- adapter_test.go
- 橋接模式
- bridge.go
- bridge_test.go
- 合成/組合模式
- composite.go
- composite_test.go
- 裝飾模式
- decoretor.go
- decorator_test.go
- 外觀模式
- facade.go
- facade_test.go
- 享元模式
- flyweight.go
- flyweight_test.go
- 代理模式
- proxy.go
- proxy_test.go
- 行為型模式
- 職責鏈模式
- chainofresponsibility.go
- chainofresponsibility_test.go
- 命令模式
- command.go
- command_test.go
- 解釋器模式
- interpreter.go
- interperter_test.go
- 迭代器模式
- iterator.go
- iterator_test.go
- 中介者模式
- mediator.go
- mediator_test.go
- 備忘錄模式
- memento.go
- memento_test.go
- 觀察者模式
- observer.go
- observer_test.go
- 狀態模式
- state.go
- state_test.go
- 策略模式
- strategy.go
- strategy_test.go
- 模板模式
- templatemethod.go
- templatemethod_test.go
- 訪問者模式
- visitor.go
- visitor_test.go
- 數據庫操作
- golang操作MySQL
- 1.mysql使用
- 2.insert操作
- 3.select 操作
- 4.update 操作
- 5.delete 操作
- 6.MySQL事務
- golang操作Redis
- 1.redis介紹
- 2.golang鏈接redis
- 3.String類型 Set、Get操作
- 4.String 批量操作
- 5.設置過期時間
- 6.list隊列操作
- 7.Hash表
- 8.Redis連接池
- 其它Redis包
- go-redis/redis包
- 安裝介紹
- String 操作
- List操作
- Set操作
- Hash操作
- golang操作ETCD
- 1.etcd介紹
- 2.鏈接etcd
- 3.etcd存取
- 4.etcd監聽Watch
- golang操作kafka
- 1.kafka介紹
- 2.寫入kafka
- 3.kafka消費
- golang操作ElasticSearch
- 1.ElasticSearch介紹
- 2.kibana介紹
- 3.寫入ElasticSearch
- NSQ
- 安裝
- 生產者
- 消費者
- zookeeper
- 基本操作測試
- 簡單的分布式server
- Zookeeper命令行使用
- GORM
- gorm介紹
- gorm查詢
- gorm更新
- gorm刪除
- gorm錯誤處理
- gorm事務
- sql構建
- gorm 用法介紹
- Go操作memcached
- beego框架
- 1.beego框架環境搭建
- 2.參數配置
- 1.默認參數
- 2.自定義配置
- 3.config包使用
- 3.路由設置
- 1.自動匹配
- 2.固定路由
- 3.正則路由
- 4.注解路由
- 5.namespace
- 4.多種數據格式輸出
- 1.直接輸出字符串
- 2.模板數據輸出
- 3.json格式數據輸出
- 4.xml格式數據輸出
- 5.jsonp調用
- 5.模板處理
- 1.模板語法
- 2.基本函數
- 3.模板函數
- 6.請求處理
- 1.GET請求
- 2.POST請求
- 3.文件上傳
- 7.表單驗證
- 1.表單驗證
- 2.定制錯誤信息
- 3.struct tag 驗證
- 4.XSRF過濾
- 8.靜態文件處理
- 1.layout設計
- 9.日志處理
- 1.日志處理
- 2.logs 模塊
- 10.會話控制
- 1.會話控制
- 2.session 包使用
- 11.ORM 使用
- 1.鏈接數據庫
- 2. CRUD 操作
- 3.原生 SQL 操作
- 4.構造查詢
- 5.事務處理
- 6.自動建表
- 12.beego 驗證碼
- 1.驗證碼插件
- 2.驗證碼使用
- beego admin
- 1.admin安裝
- 2.admin開發
- beego 熱升級
- beego實現https
- gin框架
- 安裝使用
- 路由設置
- 模板處理
- 文件上傳
- gin框架中文文檔
- gin錯誤總結
- 項目
- 秒殺項目
- 日志收集
- 面試題
- 面試題一
- 面試題二
- 錯題集
- Go語言陷阱和常見錯誤
- 常見語法錯誤
- 初級
- 中級
- 高級
- Go高級應用
- goim
- goim 啟動流程
- goim 工作流程
- goim 結構體
- gopush
- gopush工作流程
- gopush啟動流程
- gopush業務流程
- gopush應用
- gopush新添功能
- gopush壓力測試
- 壓測注意事項
- rpc
- HTTP RPC
- TCP RPC
- JSON RPC
- 常見RPC開源框架
- pprof
- pprof介紹
- pprof應用
- 使用pprof及Go 程序的性能優化
- 封裝 websocket
- cgo
- Golang GC
- 查看程序運行過程中的GC信息
- 定位gc問題所在
- Go語言 demo
- 用Go語言計算一個人的年齡,生肖,星座
- 超簡易Go語言實現的留言板代碼
- 信號處理模塊,可用于在線加載配置,配置動態加載的信號為SIGHUP
- 陽歷和陰歷相互轉化的工具類 golang版本
- 錯誤總結
- 網絡編程
- 網絡編程http
- 網絡編程tcp
- Http請求
- Go語言必知的90個知識點
- 第三方庫應用
- cli應用
- Cobra
- 圖表庫
- go-echarts
- 開源IM
- im_service
- 機器學習庫
- Tensorflow
- 生成二維碼
- skip2/go-qrcode生成二維碼
- boombuler/barcode生成二維碼
- tuotoo/qrcode識別二維碼
- 日志庫
- 定時任務
- robfig/cron
- jasonlvhit/gocron
- 拼多多開放平臺 SDK
- Go編譯
- 跨平臺交叉編譯
- 一問一答
- 一問一答(一)
- 為什么 Go 標準庫中有些函數只有簽名,沒有函數體?
- Go開發的應用
- etcd
- k8s
- Caddy
- nsq
- Docker
- web框架