首页 技术 正文
技术 2022年11月16日
0 收藏 432 点赞 2,641 浏览 1495 个字
package main
import (
"fmt"
"strings"
"strconv"
)func main(){
//返回字符串的(字节)长度,相当于PHP的strlen
str := "hello世界" //11 ,golang的编码统一为utf-8,字母和数字分别占一个字节,汉子占用3个字节
// str := "hello" //
fmt.Println(len(str)) //字符串遍历
str2 := "hello世界"
//如果有中文,需要转切片,不然会出现乱码,因为是按照字符串的字节长度遍历
str2_r := []rune(str2)
for i := ; i < len(str2_r); i++ {
fmt.Printf("%c\n",str2_r[i])
} //字符串转整数
//第一个参数是要转化成的字符串,第二个是错误信息.这里需要引用strconv包
n, err := strconv.Atoi("")
if err != nil { //如果有错
fmt.Println("转换错误", err)
} else { //如果成功
fmt.Println("转换成功", n)
} //整数转字符串
str = strconv.Itoa()
fmt.Printf("str=%v, str=%T", str, str) //str=12345, str=string //字符串转[]byte
var bytes = []byte("hello golang")
fmt.Println(bytes) //str=12345, str=string[104 101 108 108 111 32 103 111 108 97 110 103] //[]byte转字符串
str = string([]byte{, , })
fmt.Println(str) //abc 的asc码分别是97,98,99 //十进制转化成2,8,16进制,返回对应的字符串
str = strconv.FormatInt(, )
fmt.Printf("123对应的二进制是=%v\n", str)
str = strconv.FormatInt(, )
fmt.Printf("123对应的八进制是=%v\n", str)
str = strconv.FormatInt(, )
fmt.Printf("123对应的十六进制是=%v\n", str)
//123对应的二进制是=1111011
//123对应的八进制是=173
//123对应的十六进制是=7b //判断一个字符串中是否包含指定字符串 ,相当于php中的strpos,但是php中可能会返回下标
status := strings.Contains("hello world", "hello")
fmt.Printf("status=%v\n", status) //status=true //统计一个字符串中有几个指定的字符
count := strings.Count("hello world", "l")
fmt.Printf("字符存在个数:%v\n", count) //字符存在个数:3;如果没有,则0 //字符串比较:不区分大小写
status = strings.EqualFold("abc", "ABC")
fmt.Printf("status=%v\n", status) //status=true //字符串比较:区分大小写 两个=
fmt.Printf("status=%v", "abc" == "ABC")//status=false //返回子字符串在指定字符串中第一次出现的index值(位置),如果没有返回-1
index := strings.Index("test_str", "s")
fmt.Printf("index=%v", index) //index=2
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,487
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,486
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,126
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,287