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
}

Golang字符串函数认识(一)的更多相关文章

  1. Golang字符串函数认识(二)

    package main import ( "fmt" "strings" ) func main(){ //返回字符在指定字符串中最后一次出现的位置 last ...

  2. GO语言的进阶之路-Golang字符串处理以及文件操作

    GO语言的进阶之路-Golang字符串处理以及文件操作 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 我们都知道Golang是一门强类型的语言,相比Python在处理一些并发问题也 ...

  3. [转] golang 字符串比较是否相等

    1 前言 strings.EqualFold不区分大小写,"==" 区分且直观. 2 代码 golang字符串比较的三种常见方法 fmt.Println("go" ...

  4. golang(06)函数介绍

    原文链接 http://www.limerence2017.com/2019/09/11/golang11/#more 函数简介 函数是编程语言中不可缺少的部分,在golang这门语言中函数是一等公民 ...

  5. [Golang]字符串拼接方式的性能分析

    本文100%由本人(Haoxiang Ma)原创,如需转载请注明出处. 本文写于2019/02/16,基于Go 1.11.至于其他版本的Go SDK,如有出入请自行查阅其他资料. Overview 写 ...

  6. ThinkPHP 模板substr的截取字符串函数

    ThinkPHP 模板substr的截取字符串函数在Common/function.php加上以下代码 /** ** 截取中文字符串 **/ function msubstr($str, $start ...

  7. SQL字符串函数

    LEN() :计算字符串长度(字符的个数.)datalength();//计算字符串所占用的字节数,不属于字符串函数.测试varchar变量与nvarchar变量存储字符串a的区别.见备注1.LOWE ...

  8. Python3中的字符串函数学习总结

    这篇文章主要介绍了Python3中的字符串函数学习总结,本文讲解了格式化类方法.查找 & 替换类方法.拆分 & 组合类方法等内容,需要的朋友可以参考下. Sequence Types ...

  9. TSQL 字符串函数:截断和查找

    字符串截断函数是指:Stuff 和 SubString,字符串查找函数是:CharIndex 和 PatIndex 一,SubString 截取子串 最常用的字符串函数,用于截取特定长度的子串. SU ...

随机推荐

  1. [LeetCode] 613. Shortest Distance in a Line_Easy tag: SQL

    Table point holds the x coordinate of some points on x-axis in a plane, which are all integers. Writ ...

  2. MySQL--9存储引擎

    存储引擎:存储数据.查询数据的一种技术. 关系型数据库中数据是以表的形式存储的,所以存储引擎也叫表类型.

  3. css3径向渐变

    #grad2 { height: 440px; width: 440px; border-radius: %; background: -webkit-radial-gradient(closest- ...

  4. 单点登录开源架构之CAS

    服务端 开源地址:https://github.com/apereo/cas Release版:https://github.com/apereo/cas/releases Windows下使用下载c ...

  5. NPOI设置单元格格式

    转自:http://www.cr173.com/html/18143_2.html //创建一个常用的xls文件 private void button3_Click(object sender, E ...

  6. vue2.0leaflet

    github源码在此,记得点星:https://github.com/brandonxiang/vueleaflet 参考文档:https://korigan.github.io/Vue2Leafle ...

  7. es6函数的扩展

    扩展运算符 含义 扩展运算符(spread)是三个点(...).它好比 rest 参数的逆运算,将一个数组转为用逗号分隔的参数序列. 扩展运算符的应用 (1)合并数组 // ES5 [1, 2].co ...

  8. Mysql导出(多张表)表结构及表数据 mysqldump用法

        命令行下具体用法如下:  mysqldump -u用戶名 -p密码 -d 數據库名 表名 脚本名; 1.导出數據库為dbname的表结构(其中用戶名為root,密码為dbpasswd,生成的脚 ...

  9. DatabaseGenerated(转)

    在EF中,我们建立数据模型的时候,可以给属性配置数据生成选项DatabaseGenerated,它后有三个枚举值:Identity.None和Computed. Identity:自增长 None:不 ...

  10. pycharm python3.5 神奇的导入问题

    说明:pycharm目录中没有同名.py文件,则可以直接用import util导入: 若有同名.py文件,则导入的时候需要加入所在文件夹名称