当前位置:网站首页>golang 字符串操作
golang 字符串操作
2022-08-11 08:04:00 【Miss 古月先生】
字符串转义符
Go 语言的字符串常见转义符包含回车、换行、单双引号、制表符等,如下表所示
字符串的常用操作
package main
import(
"fmt"
"strings"
)
func main(){
//len 字符长度
str1 := "你好" //str的值:你好 str1的长度:6
fmt.Printf("str1的值:%v str1的长度:%v\n",str1,len(str1))
str1 := "aaa" //str1的值:aaa str1的长度:3
fmt.Printf("str1的值:%v str1的长度:%v\n",str1,len(str1))
// + 或 fmt.Sprintf 拼接
str1 := "Hello"
str2 := "World"
str3 := str1 + str1 //str3 结果=HelloWorld
str4 := fmt.Sprintf("%v %v", str1, str2) //str4 结果=Hello World
fmt.Printf("str3 结果=%v\nstr4 结果=%v\n",str3,str4)
// strings.Split 分割
str1 := "php,java,goalng"
str2 := strings.Split(str1,",")//str2的值=[php java goalng] str2的类型=[]string
fmt.Printf("str2的值=%v str2的类型=%T\n",str2,str2)
// strings.Join 拼接
str1 := "php,java,goalng"
str2 := strings.Split(str1,",")
str3 := strings.Join(str2,"-") //str3的值=php-java-goalng str3的类型=string
fmt.Printf("str3的值=%v str3的类型=%T\n",str3,str3)
// strings.contains 判断是否包含
str1 := "Hello World"
str2 := strings.Contains(str1,"ll") //str2的值=false str2的类型=bool
fmt.Printf("str2的值=%v str2的类型=%T\n",str2,str2)
// strings.HasPrefix,strings.HasSuffix 前缀/后缀判断
str1 := "Hello World"
str2 := strings.HasPrefix(str1,"He") //str2的值=true
fmt.Printf("str2的值=%v\n",str2)
str3 := strings.HasSuffix(str1,"ld") //str2的值=true
fmt.Printf("str3的值=%v",str3)
// strings.Index(),strings.LastIndex() 首次/最后 出现的位置
/* "Hello World" 012345678910 */
str1 := "Hello World"
str2 := strings.Index(str1,"o")//str2的值=4
fmt.Printf("str2的值=%v\n",str2)
str3 := strings.LastIndex(str1,"l")//str3的值=9
fmt.Printf("str3的值=%v\n",str3)
}
边栏推荐
- Kotlin算法入门求回文数数算法优化二数字生成规则
- 【实战系列】OpenApi设计规范
- 【TA-霜狼_may-《百人计划》】图形3.7.2 command buffer简
- 零基础SQL教程: 主键、外键和索引 04
- 为什么会没有内存了呢
- Creo9.0 特征的成组
- Redis source code: how to view the Redis source code, the order of viewing the Redis source code, the sequence of the source code from the external data structure of Redis to the internal data structu
- 剑指offer专项突击版第26天
- There may be fields that cannot be serialized in the abnormal object of cdc and sqlserver. Is there anyone who can understand it? Help me to answer
- One-hot in TF
猜你喜欢
1002 Write the number (20 points)
1003 I want to pass (20 points)
IQUNIX A80 exploring TTC金粉 初体验
机器学习(一)数据的预处理
4.1 - Support Vector Machines
Redis source code: how to view the Redis source code, the order of viewing the Redis source code, the sequence of the source code from the external data structure of Redis to the internal data structu
go-grpc TSL authentication solution transport: authentication handshake failed: x509 certificate relies on ... ...
3.1-Classification-probabilistic generative model
Test cases are hard?Just have a hand
笔试题大疆08.07
随机推荐
The easiest trick to support quick renaming of various files
关于Excel实现分组求和最全文档
初级软件测试工程师笔试试题,你知道答案吗?
oracle数据库中列转行,列会有变化
测试用例很难?有手就行
Redis source code-String: Redis String command, Redis String storage principle, three encoding types of Redis string, Redis String SDS source code analysis, Redis String application scenarios
Do you know the basic process and use case design method of interface testing?
1036 Programming with Obama (15 points)
项目1-PM2.5预测
零基础SQL教程: 主键、外键和索引 04
1096 big beautiful numbers (15 points)
笔试题大疆08.07
jenkins简单使用
项目2-年收入判断
机器学习(一)数据的预处理
JRS303-数据校验
1036 跟奥巴马一起编程 (15 分)
1051 Multiplication of Complex Numbers (15 points)
如何仅更改 QGroupBox 标题的字体?
剑指offer专项突击版第26天