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] 581. Shortest Unsorted Continuous Subarray_Easy tag: Sort, Stack
Given an integer array, you need to find one continuous subarray that if you only sort this subarray ...
- js树形结构-----(BST)二叉树增删查
function BinarySearchTree(){ var cnodes = function(key){ this.key = key; this.left = null; this.righ ...
- php curl POST multipart/form-data与application/x-www-form-urlencode的区别
背景 CURL在 a.php 中以 POST方式向 b.php 提交数据,但b.php无法接收到数据,而 CURL 操作显示成功. 原来,"传递一个数组到CURLOPT_POSTFIELDS ...
- 解决mysql的内存表“table is full”错误
最后参考http://blog.sina.com.cn/s/blog_6942a1590101429h.html 来解决,摘录下核心 后来GOOGLE得知,需要重建该表才可以. 1. 设置新的参数 m ...
- Boost学习-Linuxidc上的很好的学习资料
来自 http://www.linuxidc.com/Linux/2011-07/39215.htm,拷贝第一页如下 Boost学习系列 简介及基本用法 [日期:2011-07-25] 来源:Linu ...
- iOS LeftMenu抽屉效果与ScrollView共存时的手势冲突
公司有个项目,需要做左侧滑动,首页是ScrollView嵌套TableView.首页是一个ScrollView,所以当contentOffset是0.0的时候,无法直接滑动出抽屉效果,用户体验感非常差 ...
- Web API 入门 一
因为只是是一个简单的入门.所有暂时不去研究web API一些规范.比如RESTful API 这里有个接收RESTful API的.RESTful API 什么是WebApi 看这里:http://w ...
- beego 初体验 - 环境搭建
首先,安装go运行时和beego beego,在git bash 运行命令: go get github.com/beego/bee go get github.com/astaxie/beego g ...
- 10.for
要遍历一个范围(如数组或表),使用 for 循环.在数组上迭代时,当前数组元素存储在循环变量中.在遍历字典时, index 存储在循环变量中. (in 内容测试) for x in [5, 7, 11 ...
- Sql Server参数化查询之where in和like实现详解 [转]
文章导读 拼SQL实现where in查询 使用CHARINDEX或like实现where in 参数化 使用exec动态执行SQl实现where in 参数化 为每一个参数生成一个参数实现where ...