当前位置:网站首页>golang string manipulation
golang string manipulation
2022-08-11 08:13:00 【Miss Gu Yue】
字符串转义符
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)
}
边栏推荐
- 链式编程注解
- 《剑指offer》题解——week3(持续更新)
- About # SQL problem: how to set the following data by commas into multiple lines, in the form of column display
- Find the latest staff salary and the last staff salary changes
- 小目标检测3_注意力机制_Self-Attention
- 1046 punches (15 points)
- 经典论文-MobileNet V1论文及实践
- 查询跟踪快递单号物流,智能分析物流中转有延误的单号
- 2021-08-11 For loop combined with multi-threaded asynchronous query and collect results
- nodejs微服务中跨域,请求,接口,参数拦截等功能
猜你喜欢

oracle数据库中列转行,列会有变化

Write a resume like this, easy to get the interviewer

少年成就黑客,需要这些技能

excel 透视表 值显示内容 不显示计数

Do you know the basic process and use case design method of interface testing?

Analysys and the Alliance of Small and Medium Banks jointly released the Hainan Digital Economy Index, so stay tuned!

The growth path of a 40W test engineer with an annual salary, which stage are you in?

Creo9.0 特征的成组

项目1-PM2.5预测

1061 True or False (15 points)
随机推荐
kali渗透测试环境搭建
Write a resume like this, easy to get the interviewer
Pico neo3 Unity Packaging Settings
【43. 字符串相乘】
麒麟V10系统打包Qt免安装包程序
兼容并蓄广纳百川,Go lang1.18入门精炼教程,由白丁入鸿儒,go lang复合容器类型的声明和使用EP04
2.1 - Gradient Descent
关于#sql#的问题:怎么将下面的数据按逗号分隔成多行,以列的形式展示出来
Hibernate 的 Session 缓存相关操作
Decrement operation in tf; tf.assign_sub()
1.1-Regression
Analysys and the Alliance of Small and Medium Banks jointly released the Hainan Digital Economy Index, so stay tuned!
用 Antlr 重构脚本解释器
pyqt5实现仪表盘
Kotlin算法入门兔子数量优化及拓展
Nuget找不到包的问题处理
快速幂,逆元的求解
magical_spider远程采集方案
C语言操作符详解
Unity3D——自定义类的Inspector面板的修改