当前位置:网站首页>字符串操作
字符串操作
2022-07-07 05:27:00 【Jimmy_jimi】
package main
import (
"fmt"
"strconv"
"strings"
//"go/types"
)
func main() {
str := "hellow元素"
//统计长度
fmt.Println(len(str))
//字符串遍历
r := []rune(str)
for i:=0;i<len(r);i++{
fmt.Printf("%c ",r[i])
}
//字符串转整数
n,_ := strconv.Atoi("12")
fmt.Printf("\ntype:%T,date:%d",n,n)
//整形转字符串
//%s 字符串,%v内置格式内容
//%v 输出所有值,%+v输出字段类型和值,%#V 输出结构体名字和结构体
m := strconv.Itoa(1234)
fmt.Printf("\ntype:%T,date:%v",m,m)
//字符串转[]byte
//byte 是uint8的别名,表示只占一个字节的ASCII码
println(" ")
var bytes = []byte("hellow go")
fmt.Printf("%v",bytes)
//判断是否存在字串
fmt.Printf("\n是否存在字串:%v",strings.Contains("safes","sa"))
//统计有几个字串
fmt.Printf("\n%v个字串",strings.Count("safeeeees","e"))
//字符转比较
// "=="区分大小写,EqualFold不区分
fmt.Printf("\n%v",strings.EqualFold("abc","AbC"))
fmt.Println("abc"=="aBc")
//返回字串第一次和最后一次出现的位置,没有返回-1
fmt.Printf("%v",strings.Index("aaaaaa","a"))
fmt.Printf("\n%v",strings.LastIndex("aaaaaa","a"))
//替换,n指像替换的数量,-1为全部
fmt.Printf("\n%v",strings.Replace("go to learn go","go","c 语言",-1))
//拆分,结果为字符转数组
fmt.Printf("\n%v",strings.Split("go to learn go" ," "))
//判断是否以字串开始/结束
fmt.Printf("\n%v",strings.HasPrefix("sdqwe.log","s"))
fmt.Printf("\n%v",strings.HasSuffix("sdqwe.log","log"))
}
边栏推荐
猜你喜欢
随机推荐
Basic use of CTF web shrink template injection nmap
Ebpf cilium practice (1) - team based network isolation
Qinglong panel - today's headlines
Lua programming learning notes
Explore creativity in steam art design
漏洞复现-Fastjson 反序列化
机器人教育在动手实践中的真理
Domain specific language / DSL in kotlin
The use of generics and vararg variable parameters in kotlin
CTF-WEB shrine模板注入nmap的基本使用
XCiT学习笔记
Golang 编译约束/条件编译 ( // +build <tags> )
Practice of combining rook CEPH and rainbow, a cloud native storage solution
GFS分布式文件系统
接口作为参数(接口回调)
IP-guard助力能源企业完善终端防泄密措施,保护机密资料安全
The truth of robot education in hands-on practice
Tuowei information uses the cloud native landing practice of rainbow
Avatary's livedriver trial experience
Snyk 依赖性安全漏洞扫描工具