Golang字符串函数认识(二)
package main
import (
"fmt"
"strings" ) func main(){
//返回字符在指定字符串中最后一次出现的位置
last_index := strings.LastIndex("Hello World", "l")
fmt.Printf("last_index=%v\n", last_index) //last_index=9 //字符串替换,类似php的str_replace,但是go的 貌似更强大
//strings.Replace("hello,welcome come go world,go to go", "golang", n)
//将指定的字符串替换成另一个子串,可以指定希望替换几个,如果n=-1表示全部替换
new_str := strings.Replace("hello,welcome come go world,go to go", "go", "golang", )
fmt.Printf("new_str=%v\n", new_str)
//last_index=9new_str=hello,welcome come golang world,golang to golang new_str = strings.Replace("hello,welcome come go world,go to go", "go", "golang", )
fmt.Printf("new_str=%v\n", new_str)
//last_index=9new_str=hello,welcome come golang world,go to go //将字符串按照指定字符分割成数组,类似php中的explode
str2arr := strings.Split("hello,golang,I love you", ",")
fmt.Printf("str2arr类型%T,%v\n", str2arr, str2arr) //str2arr类型[]string,[hello golang I love you]
for i := ; i < len(str2arr); i++ {
fmt.Printf("str2arr[%v]=%v\n", i, str2arr[i])
}
//str2arr[0]=hello
//str2arr[1]=golang
//str2arr[2]=I love you //将字符串进行大小写转换
str := "hello Golang"
str = strings.ToLower(str) //全部转换为小写
fmt.Printf("last=%v", str) //last=hello golang
str = strings.ToUpper(str) //全部转换为大写
fmt.Printf("last=%v\n", str) //last=HELLO GOLANG //将字符串两边的空格去掉,类似php中的trim
trim_str := strings.TrimSpace(" hello golang i love you ")
fmt.Printf("last=%q", trim_str) //last="hello golang i love you" //将字符串左右两边指定的字符串去掉
str = strings.Trim("~#hello go lang%#~", "#~") //第二个参数可以写多个字符
fmt.Printf("last=%v" ,str) //last=hello go lang% //将字符串左边的指定字符去掉|将字符串右边的指定字符去掉
strings.TrimLeft() | strings.TrimRight() //判断字符串是否是指定字符串的开头
b := strings.HasPrefix("http://192.168.0.1", "http") //true
fmt.Printf("bool=%b" ,b) //bool= true
//判断字符串止否是指定字符串的结尾
strings.HasSuffix("test.png", "jpg") //false }
package main import "fmt"
import "strings" func main() { //Joins 组合
s := []string{"abc", "def", "ghi", "lmn"}
buf := strings.Join(s, "---")
fmt.Println("buf = ", buf) // abc---def---ghi---lmn //重复次数拼接
buf = strings.Repeat("go", )
fmt.Println("buf = ", buf) //"gogogo" //去掉空格,把元素放入切片中
s3 := strings.Fields(" are u ok? ")
//fmt.Println("s3 = ", s3)
for i, data := range s3 {
fmt.Println(i, ", ", data)
//0 , are
//1 , u
//2 , ok?
}
}
Golang字符串函数认识(二)的更多相关文章
- MATLAB常用字符串函数之二
1,lower和upper lower: 将包含的全部字母转换为小写. upper: 将包含的全部字母转化为大写. 实例一: >>str='Sophia is a good girl.'; ...
- Golang字符串函数认识(一)
package main import ( "fmt" "strings" "strconv" ) func main(){ //返回字符串 ...
- 13-C语言字符串函数库
目录: 一.C语言字符串函数库 二.用命令行输入参数 回到顶部 一.C语言字符串函数库 1 #include <string.h> 2 字符串复制 strcpy(参数1,参数2); 参数1 ...
- 【C++实现python字符串函数库】二:字符串匹配函数startswith与endswith
[C++实现python字符串函数库]字符串匹配函数startswith与endswith 这两个函数用于匹配字符串的开头或末尾,判断是否包含另一个字符串,它们返回bool值.startswith() ...
- C语言-字符串函数的实现(二)之strcpy
C语言中的字符串函数有如下这些 获取字符串长度 strlen 长度不受限制的字符串函数 strcpy strcat strcmp 长度受限制的字符串函数 strncpy strncat strncmp ...
- GO语言的进阶之路-Golang字符串处理以及文件操作
GO语言的进阶之路-Golang字符串处理以及文件操作 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 我们都知道Golang是一门强类型的语言,相比Python在处理一些并发问题也 ...
- Go 函数,包(二)
#### Go 函数,包(二)***百丈峰,松如浪,地势坤,厚德载物之像*** 今天又到周五啦,你们有没有激动呢,反正我很激动,又有两天的自由了; 上一节我们学习了Go 的函数和包的一些知识 , 今天 ...
- TSQL 字符串函数:截断和查找
字符串截断函数是指:Stuff 和 SubString,字符串查找函数是:CharIndex 和 PatIndex 一,SubString 截取子串 最常用的字符串函数,用于截取特定长度的子串. SU ...
- c#编程基础之字符串函数
c#常用的字符串函数 例一: 获取字符串的大小写函数 ToLower():得到字符串的小写形式 ToUpper():得到字符串的大写形式 注意: 字符串时不可变的,所以这些函数都不会直接改变字符串的内 ...
随机推荐
- [LeetCode] 762. Prime Number of Set Bits in Binary Representation_Easy
Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime ...
- Firewalld防火墙:端口转发与流量均衡
firewalld和iptables的关系 firewalld自身并不具备防火墙的功能,而是和iptables一样需要通过内核的netfilter来实现,也就是说firewalld和iptables一 ...
- Mysql删除重复记录,保留id最小的一条
mysql 查询重复字段,及删除重复记录的方法MySQL, 数据库, 数据库, 字段, 服务器数据库中有个大表,需要查找其中的名字有重复的记录id,以便比较.如果仅仅是查找数据库中name不重复的字段 ...
- cocos游戏开发小白教程网站
<Quick-Cocos2d-x v3.3小白书系列教程> <Quick-Cocos2d-x初学者游戏教程>
- Nginx加载ngx_pagespeed模块,加快网站打开的速度
[页面加速]配置Nginx加载ngx_pagespeed模块,加快网站打开的速度 ngx_pagespeed 是一个 Nginx 的扩展模块,可以加速你的网站,减少页面加载时间,它会自动将一些提升 ...
- 第零章 HTML启蒙知识与网站开发流程
Web前端启蒙知识:1.软件架构模式a)B/S架构:Browser-Server 浏览器服务器模型b)C/S架构:Client-Server 客户端服务器模型注1:浏览器是运行网页的应用程序注2:B/ ...
- HAProxy实现mysql负载均衡
安装 yum install haproxy 修改配置 vi /etc/haproxy/haproxy.cfg 配置如下 global daemon nbproc 1 pidfile /var/r ...
- url中是否加斜杠/
通常来说,不加斜杠的形式(如”example.jsp”)请求的是相对于当前页面路径的资源 http://localhost:8080/webapp/examole:加斜杠的形式(”/example.j ...
- yum 与 apt 的对比
一.概念 使用yum/apt之前,你很可能会遇到配置源(ubuntu下一般内置的就比较好,所以可能很少人手动配置),那这个源是什么呢,就是告诉apt/yum,安装软件的时候你要从哪里下载.比如你使用1 ...
- 在Hue中提交oozie定时任务
可以参见下面这篇博文: 通过hue提交oozie定时任务