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字符串函数认识(二)的更多相关文章

  1. MATLAB常用字符串函数之二

    1,lower和upper lower: 将包含的全部字母转换为小写. upper: 将包含的全部字母转化为大写. 实例一: >>str='Sophia is a good girl.'; ...

  2. Golang字符串函数认识(一)

    package main import ( "fmt" "strings" "strconv" ) func main(){ //返回字符串 ...

  3. 13-C语言字符串函数库

    目录: 一.C语言字符串函数库 二.用命令行输入参数 回到顶部 一.C语言字符串函数库 1 #include <string.h> 2 字符串复制 strcpy(参数1,参数2); 参数1 ...

  4. 【C++实现python字符串函数库】二:字符串匹配函数startswith与endswith

    [C++实现python字符串函数库]字符串匹配函数startswith与endswith 这两个函数用于匹配字符串的开头或末尾,判断是否包含另一个字符串,它们返回bool值.startswith() ...

  5. C语言-字符串函数的实现(二)之strcpy

    C语言中的字符串函数有如下这些 获取字符串长度 strlen 长度不受限制的字符串函数 strcpy strcat strcmp 长度受限制的字符串函数 strncpy strncat strncmp ...

  6. GO语言的进阶之路-Golang字符串处理以及文件操作

    GO语言的进阶之路-Golang字符串处理以及文件操作 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 我们都知道Golang是一门强类型的语言,相比Python在处理一些并发问题也 ...

  7. Go 函数,包(二)

    #### Go 函数,包(二)***百丈峰,松如浪,地势坤,厚德载物之像*** 今天又到周五啦,你们有没有激动呢,反正我很激动,又有两天的自由了; 上一节我们学习了Go 的函数和包的一些知识 , 今天 ...

  8. TSQL 字符串函数:截断和查找

    字符串截断函数是指:Stuff 和 SubString,字符串查找函数是:CharIndex 和 PatIndex 一,SubString 截取子串 最常用的字符串函数,用于截取特定长度的子串. SU ...

  9. c#编程基础之字符串函数

    c#常用的字符串函数 例一: 获取字符串的大小写函数 ToLower():得到字符串的小写形式 ToUpper():得到字符串的大写形式 注意: 字符串时不可变的,所以这些函数都不会直接改变字符串的内 ...

随机推荐

  1. jenkins 常用插件和配置项介绍和使用

    jenkins 上搜索不到的插件可以在如下地址下载: http://updates.jenkins-ci.org/download/plugins/ 1.Notification Plugin 介绍: ...

  2. [LeetCode] 124. Binary Tree Maximum Path Sum_ Hard tag: DFS recursive, Divide and conquer

    Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...

  3. Express web框架

    哈哈,还是Node.JS哦 现在我们来看看位Node.JS些的Express webkuangjia 一. 安装express npm install express -g --save npm in ...

  4. Selenium基础知识(七)弹出框处理

    使用switch_to.alert方法来处理弹页面弹出的警告框 页面常见警告框种类:alert/confirm 确认框/prompt switch_to.alert().accept() switch ...

  5. RabbitMq入门详解

    因为项目中需要用到RabbitMq,所有花时间研究了下,虽然博客园已经有前辈写了关于RabbitMq的文章.但还是有必要研究下! 什么是RabbitMq? 百度解释:MQ全称为Message Queu ...

  6. 本地tp项目上传服务器报runtime/cache错误

    很简单,给runtime权限777 就好了 chmod -r 777 runctime

  7. OBV15 案例5,上M10拉高出货

  8. Mongodb内嵌数组的完全匹配查询

    样例数据: {      "cNo" : "11",     "Details" : [         {              &q ...

  9. 20180309 - C# demo - 1

    using System; namespace HelloWorldApplication { class HelloWorld { static void Main(string[] args) { ...

  10. spring boot 知识点

    spring boot 好处 1. 简化配置,spring boot 提供了默认配置 例如 日志 默认logback日志  info级别 2. 简化部署,内嵌容器,tomcat,jetty,直接部署j ...