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每天一题】Palindrome Number( 回文数字)
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same back ...
- 【LeetCode每天一题】Median of Two Sorted Arrays(两数组中的中位数)
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the tw ...
- jenkins 常见问题汇总
1.jenkins api调用 https://my.oschina.net/sanpeterguo/blog/197931 其中,有个错误的地方,带参数构建时,使用的时POST方法,如下: curl ...
- CentOS6.5安装sqoop2
1.下载软件:http://archive.cloudera.com/cdh5/cdh/5/ 2.解压:tar -zxvf mysofts/sqoop2-1.99.5-cdh5.6.0.tar.gz ...
- [xdoj]1303jlz的刷题黑科技
先分析复杂度,给的数据是1e5的,那么我们至少需要一个nlogn的算法才可以.由于答案是一个数字,首先想到是二分法(一般答案是一个数字都可以通过二分法来完成) 下面是思路: 1.可以完成题目的条件是, ...
- 从零开始一起学习SLAM | 神奇的单应矩阵
小白最近在看文献时总是碰到一个奇怪的词叫“homography matrix”,查看了翻译,一般都称作“单应矩阵”,更迷糊了.正所谓:“每个字都认识,连在一块却不认识”就是小白的内心独白.查了一下书上 ...
- Hibernate框架的第四天
## Hibernate框架的第四天 ## ---------- **回顾:Hibernate框架的第三天** 1. 一对多关联关系映射 * JavaBean的编写 * 编写映射的配置文件 * 使用级 ...
- javascript获取style兼容性问题
获取css 样式的方法有三种 : style, currentStyle , getComputedStyle style (无兼容性问题) 获取语法: ele.style.attr : 设置语法:e ...
- Python二分法查找
1.1二分前提是有序,,否则不可以2分,2分查找的时间复杂度是O(log n):排序后二分查找到适当的位置插入数值 lst = [37,99,73,48,47,40,40,25,99,51] def ...
- CentOS下nginx+php的配置及nginx开机启动配置
关闭防火墙 (不然外链接是访问不了 apache) service iptables stop 关闭安全系统 SELinux( 不然报403 访问页面错误 ) 1.Nginx安装主要在于配置文件的修改 ...