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开始 转义字符字符串的格式化输出切片常用函 ...
随机推荐
- MySQL☞左外链接与右外连接
外链接查询:即要查询有关联关系的数据,还要查询没有关联关系的数据.(个人理解为:表A和表B两个关联的列中)如下图: emmm,简单的来说两个表的关联关系:book.bid=bookshop.id,他们 ...
- List Leaves 树的层序遍历
3-树2 List Leaves (25 分) Given a tree, you are supposed to list all the leaves in the order of top do ...
- Vyatta 网络操作系统
原文发表于:2010-09-19 转载至cu于:2012-07-21 以下是"开源中国社区"写到的: http://www.oschina.net/news/11423/vyatt ...
- 三点须知:当我们在开发过程中需要用到分布式缓存Redis的时候
当我们在开发过程中需要用到分布式缓存Redis的时候,我们首先要明白缓存在系统中用来做什么? 1. 少量数据存储,高速读写访问.通过数据全部in-momery 的方式来保证高速访问,同时提供数据落地的 ...
- Halcon如何保存仿射变换矩阵
这里我们通过序列化来实现的,如下图,写到硬盘的HomMat2D_1内容和从硬盘里HomMat2D_2读出的内容一致,源代码在图片下方. Halcon源代码: hom_mat2d_identity (H ...
- CSS 实用实例
背景颜色 1. 颜色背景 <style type="text/css">body { font-size: 16px;">h1 { font-size: ...
- LeetCode 888. Fair Candy Swap(C++)
题目: Alice and Bob have candy bars of different sizes: A[i] is the size of the i-th bar of candy that ...
- c# HttpListener拒绝访问
直接记录解决步骤: 程序代码: HttpListener httpListener = new HttpListener(); httpListener.Prefixes.Add("http ...
- 【并查集】 不相交集合 - 并查集 教程(文章作者:Slyar)
最近写了一个多星期的并查集,一瞬间贴出这么多解题报告,我想关于并查集的应用先告一段落吧,先总结一下. 在网上看到一篇关于并查集比较好的教程(姑且允许我这么说吧),不转过来是在可惜.献给爱学习的你 文章 ...
- 02_Java基础_第2天(变量、运算符)_讲义
今日内容介绍 1.变量 2.运算符 01变量概述 * A: 什么是变量? * a: 变量是一个内存中的小盒子(小容器),容器是什么?生活中也有很多容器, * 例如水杯是容器,用来装载水:你家里的大衣柜 ...