golang字符串常用函数
package utils
import "fmt"
import "strconv"
import "strings"
var str string = "学golang使我快乐!"
// 关于string的常用函数
// 1. len(str) 内建函数,返回字符串长度,按字节,1个汉字3字节
// 2. range []rune(str) 字符串遍历,处理中文问题
func F2() {
for _,value := range []rune(str) {
fmt.Printf("%c \n",value)
}
}
// 3. string转整数 strconv.Atoi(str) 这个函数是 strconv.ParseInt(s string, base int, bitSize int) (i int64 err error)的简化版
func F3() {
num, _ := strconv.Atoi("666")
fmt.Printf("num type is %T,value is %v", num, num) // num type is int,value is 666
}
// 4. 整数转string strconv.Itoa(666) 是strconv.FormatInt(i int64, base int) string的简化版
// 5. string to []byte b := []byte(str)
func F5(){
b := []byte(str)
fmt.Printf("%T %v\n", b, b) // type of b is []uint8
}
// 6. []byte to string s := string([]byte{77,88,99})
func F6(){
s := string([]byte{77,88,99})
fmt.Printf("%T %v\n", s, s) // string MXc
}
// 7. 十进制数转2 8 16进制字符串 strconv.FormatInt(i int64, base int) string base->2,8,16
// 8. 判断字符串s是否包含子串substr strings.Contains(s, substr string) bool
// 9. 统计子串出现次数 strings.Count(s, sep string) int
func F9() {
s := "A man who helps you when you are in trouble and who leaves you when you are successful is a real friend."
fmt.Println(strings.Count(s,"you")) // 4
s = "lv"
fmt.Println(strings.Count(s,"")) // 3
}
// 10. 判断连个字符串是否相等 str1 == str2 区分大小写, 不区分大小写方式strings.EqualFold(s, t string) bool
// 11. 子串sep在字符串s中第一次/最后一次出现的位置,不存在则返回-1 Index(s, sep string) int/LastIndex(s, sep string) int
// 12. 将n个old子串替换为new字符串,n<0会替换所有old子串 strings.Replace(s, old, new string, n int) string
// 13. 大小写转换 strings.ToUpper /ToLower
// 14. 按sep拆分字符串,返回一个slice strings.Split(s, sep string) []string
// 15. 将字符串slice以sep为分隔符组合成一个字符串 strings.Join(a []string, sep string) string
// 16. Trim系列 Trim(s string, cutset string) string 去除左右两边指定字符串 TrimRight/TrimLeft
// TrimSpace(s string) string 去除左右两边空白
// TrimPrefix(s, prefix string) string /TrimSuffix(s, suffix string) string 去除前/后缀
// 17. 判断s是否有前缀/后缀字符串prefix HasPrefix(s, prefix string) bool / HasSuffix
golang字符串常用函数的更多相关文章
- Excel怎样从一串字符中的某个指定“字符”前后截取字符及截取字符串常用函数
怎么样可以从一串字符中的某个指定位置的前或后截取指定个数的字符. 如:12345.6789,我要截取小数点前(或后)的3个字符.怎么样操作, 另外,怎么样从右边截取字符,就是和left()函数相反的那 ...
- python的基本用法(三)字符串常用函数
字符串常用函数 # s='.abcd.'# new_s=s.strip('.')#默认去掉字符串两边的空格和换行符,想去掉什么括号中就写什么# print('s',s)# print('new_s', ...
- python字符串 常用函数 格式化字符串 字符串替换 制表符 换行符 删除空白 国际货币格式
# 字符串常用函数# 转大写print('bmw'.upper()) # BMW# 转小写print('BMW'.lower()) # bmw# 首字母大写print('how aae you ?'. ...
- SQL Server中截取字符串常用函数
SQL Server 中截取字符串常用的函数: .LEFT ( character_expression , integer_expression ) 函数说明:LEFT ( '源字符串' , '要截 ...
- C 字符/字符串常用函数
string.h中常用函数 char * strchr(char * str ,char ch); 从字符串str中查找首次出现字符ch的位置,若存在返回查找后的地址,若不存在则返回NULL void ...
- python面试题(二)字符串常用函数
今天在微信的公众号上看到了一遍python学习开发的文章,里面有一些python的面试题,碰巧最近python不知道学什么了,索性学一下这篇文章啊!!先写一下一些字符串的常用函数.(ps:本人太菜,若 ...
- Python 字符串常用函数
操作字符串的常用函数 函数 描述(返回值) str.capitalize() 将字符串的第一个字符大写 str.title() 返回标题化的字符串,即每个单词的首字母都大写 str.upper() 全 ...
- php 字符串常用函数
数组.字符串和数据库是我们函数里面最.最.最常用的三类函数. 当然PHP的字符串函数也有很多.我们最常使用的两个系列的字符串: 1.单字节字符串处理函数 2.多字节字符串处理函数 3.字符串编码转换函 ...
- Python—字符串常用函数
Python-字符串常用字符串 字符串是一种表示文本的数据类型,使用单引号和双引号及三引号表示 访问字符串中的值字符串的每个字符都对应一个下标,下标编号是从0开始 转义字符字符串的格式化输出切片常用函 ...
随机推荐
- SQL中NULL的妙用
商品表Products 库房表WarehouseDistrict 库存表WarehouseStock 一般写法 ;WITH stock AS ( SELECT DistrictId, ProductI ...
- Qt中容器类应该如何存储对象
Qt提供了丰富的容器类型,如:QList.QVector.QMap等等.详细的使用方法可以参考官方文档,网上也有很多示例文章,不过大部分文章的举例都是使用基础类型:如int.QString等.如果我们 ...
- IT视频课程集
马哥Linux培训视频课程:http://pan.baidu.com/s/1pJwk7dp Oracle.大数据系列课程:http://pan.baidu.com/s/1bnng3yZ 天善智能BI培 ...
- python单元测试之参数化
paramunittest下载地址:https://pypi.python.org/pypi/ParamUnittest/ 当然我们也可以通过pip install paramunittest方式进行 ...
- 20162328蔡文琛 Java课程总结
20162328 2016-2017-2<程序设计与数据结构>课程总结 一.每周作业.结对编程博客的链接汇总 预备作业01 20162328:表达对专业的期许.浅谈师生关系.对未来学习任务 ...
- HDU 5642 King's Order dp
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5642 King's Order Accepts: 381 Submissions: 1361 ...
- 用vs调试项目的时候报HTTP 错误 403.14 - Forbidden
曾经遇到过这种诡异的问题,你一定想不到,这个可能是因为你用svn合并的时候,导致了你的dll文件出了问题. 竟然可以用主干的dll替换的方式,解决掉这个问题.
- lintcode-413-反转整数
413-反转整数 将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 0 (标记为 32 位整数). 样例 给定 x = 123,返回 321 给定 x = -123,返回 -321 标签 整数 ...
- MDL数据结构
微软的文档里对MDL的描述感觉语焉不详,这两天在找工作的间隙逆向+黑盒测试了一下MmBuildMdlForNonPagedPool,把得到的一些理解描述下来. 一.MDL数据结构 MDL是用来建立一块 ...
- springMVC 流程
springMVC流程控制 SpringMVC流程 web.xml 中配置 org.springframework.web.servlet.DispatcherServlet 这一步其实和spring ...