当前位置:网站首页>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)
}
边栏推荐
猜你喜欢
1081 Check Password (15 points)
Four operations in TF
2.1-梯度下降
1003 I want to pass (20 points)
Distributed Lock-Redission - Cache Consistency Solution
The easiest trick to support quick renaming of various files
FPGA 20个例程篇:11.USB2.0接收并回复CRC16位校验
初级软件测试工程师笔试试题,你知道答案吗?
1056 Sum of Combinations (15 points)
My creative anniversary丨Thank you for being with you for these 365 days, not forgetting the original intention, and each is wonderful
随机推荐
AUTOSAR从入门到精通番外篇(八)-C语言常用技巧50个
leetcode:69. x 的平方根
1.2 - error sources
TF generates (feature, label) set through feature and label, tf.data.Dataset.from_tensor_slices
基于微信小程序的租房小程序
matrix multiplication in tf
tf.reduce_mean() and tf.reduce_sum()
零基础SQL教程: 基础查询 05
CIKM 2022 AnalytiCup Competition: Federal Heterogeneous Task Learning
Write a resume like this, easy to get the interviewer
囍楽cloud task source code
My creative anniversary丨Thank you for being with you for these 365 days, not forgetting the original intention, and each is wonderful
go-grpc TSL authentication solution transport: authentication handshake failed: x509 certificate relies on ... ...
1003 I want to pass (20 points)
你有对象类,我有结构体,Go lang1.18入门精炼教程,由白丁入鸿儒,go lang结构体(struct)的使用EP06
Active users of mobile banking grew rapidly in June, hitting a half-year high
Two startup methods and differences of Service
Four startup modes of Activity
Kaldi语音识别工具编译问题记录(踩坑记录)
流式结构化数据计算语言的进化与新选择