Go strings

Go 的 strings 包中包含许多处理字符串的函数

官方文档:https://golang.org/pkg/strings/

前缀、后缀

判断字符串前缀、后缀

// 判断字符串 s 是否以 prefix 开头
func HasPrefix(s, prefix string) bool // 判断字符串 s 是否以 suffix 结尾
func HasSuffix(s, suffix string) bool

查找子字符串

查找字符串中是否包含子字符串

// 查找字符串 s 中是否包含 substr
func Contains(s, substr string) bool // 查找字符串 s 中是否包含 chars 中任意字符
func ContainsAny(s, chars string) bool

查找字符串中子字符串的位置

// 查找字符串 s 中第一次出现 substr 的位置,如果不存在返回 -1
func Index(s, substr string) int // 查找字符串 s 中最后一次出现 substr 的位置,如果不存在返回 -1
func LastIndex(s, substr string) int // 查找字符串 s 中第一次出现 chars 中任意字符的位置,如果不存在返回 -1
func IndexAny(s, chars string) int // 查找字符串 s 中最后一次出现 chars 中任意字符的位置,如果不存在返回 -1
func LastIndexAny(s, chars string) int // 查找字符串 s 中第一次出现 c 字节的位置,如果不存在返回 -1
func IndexByte(s string, c byte) int

修剪

Trim 通过 Unicode 编码是否一致判断去除的片段,cutset 中包含的是要去除的字节,顺序不一致也会被删除

Trim 会连续去除,直至指定位置不存在该片段,但是 TrimPrefixTrimSuffix 只会去除一次,用于去除前、后缀

// 返回字符串 s 在前后分别去除 cutset 中包含字节的切片
func Trim(s string, cutset string) string // 返回字符串 s 在前后分别去除空格的切片
// 等于 func Trim(s string, “ ”) string
func TrimSpace(s string) string // 返回字符串 s 在开头去除 cutset 中包含字节的切片
func TrimLeft(s string, cutset string) string // 返回字符串 s 在结尾去除 cutset 中包含字节的切片
func TrimRight(s string, cutset string) string

示例:

package main

import (
"fmt"
"strings"
) func main() {
a := "hello world"
b := strings.TrimLeft(a, "lhed")
c := strings.TrimPrefix(a, "hel")
d := strings.Trim(a, "lhed")
fmt.Println(a)
fmt.Println(b)
fmt.Println(c)
fmt.Println(d)
}

输出结果:

hello world
o world
lo world
o wor

分割

将一个字符串以指定分隔符进行分隔,返回一个切片

// 以一个或多个空格为分隔符,对字符串 s 进行分割,空字符串会被舍弃
func Fields(s string) []string // 以一个字符串为分隔符,对字符串 s 进行分割
// 如果分隔符为空,则将每个字符进行切割
func Split(s, sep string) []string // 在一个字符串 sep 之后对字符串 s 进行分割
// 如果字符串 sep 为空,则对每个字符进行切割
func SplitAfter(s, sep string) []string // 以一个字符串为分隔符,从第一个分隔符开始,将字符串 s 最多分割成 n 部分
// 当 n == 0 时,返回 nil;当 n == -1 时,相当于 `Split`
func SplitN(s, sep string, n int) []string // 和 `SplitAfter` 的区别相当于 `Split` 和 `SplitN`
func SplitAfterN(s, sep string, n int) []string

示例:

package main

import (
"fmt"
"strings"
) func main() {
a := " hello, world, !"
b := strings.Fields(a)
c := strings.Split(a, " ")
d := strings.Split(a, ",")
e := strings.SplitAfter(a, ",")
f := strings.SplitAfterN(a, ",", 2) fmt.Println(b, len(b))
fmt.Println(c, len(c))
fmt.Println(d, len(d))
fmt.Println(e, len(e))
fmt.Println(f, len(f))
}

输出结果:

[hello, world, !] 3
[ hello, world, !] 5
[ hello world !] 3
[ hello, world, !] 3
[ hello, world, !] 2

替换

替换会搜素字符串中所有符合条件的子字符串,之后将其替换为新字符串

// 将字符串 s 中包含的 old 字符串替换为 new 字符串,从最左边开始一共替换 n 次
// 如果 old 为空,则从字符串开头开始,字符串开头结尾和每个字符之间添加一个 new,一共添加 n 个
// 如果 n < 0 则替换次数没有限制,相当于 `ReplaceAll`
func Replace(s, old, new string, n int) string // 和 `Replace` 相似,没有限制
func ReplaceAll(s, old, new string) string

示例:

package main

import (
"fmt"
"strings"
) func main() {
a := " hello world "
b := strings.Replace(a, " ", ",", 1)
c := strings.Replace(a, "", ",", 3)
d := strings.Replace(a, " ", ",", -1)
e := strings.ReplaceAll(a, " ", ",") fmt.Println(b)
fmt.Println(c)
fmt.Println(d)
fmt.Println(e)
}

输出结果:

,hello world
, ,h,ello world
,hello,world,
,hello,world,

计数

// 字符串 s 包含子字符串 substr 的数量
// 如果 substr 为空,则返回字符串长度 + 1(字符间隙的数量)
func Count(s, substr string) int

重复

// 将字符串 s 重复 count 次,拼接成一个字符串
// count < 0 时会 panic
func Repeat(s string, count int) string

大小写转换

// 将字符串内所有字母转换成小写
func ToLower(s string) string // 将字符串中所有字母转换成大写
func ToUpper(s string) string // 每个单词的第一个字母转换为标题大小写
func Title(s string) string // 将字符串内所有字母转换为标题大小写
func ToTitle(s string) string

拼接

拼接是切割的反操作

// 以 sep 为分隔符,将 a 中元素拼接起来
func Join(a []string, sep string) string

Go package: strings的更多相关文章

  1. golang程序编译时提示“package runtime: unrecognized import path "runtime" (import path does not begin with hostname)”

    在编译golang的工程时提示错误的, 提示的错误信息如下: package bytes: unrecognized import path "bytes" (import pat ...

  2. JAVA中的字符串小结

    String字符串是只读的,不可变的 查看String类的源码,可以发现String类是被final关键字修饰的: 另外还可以看下String类源码中的其它方法实现,随便举个可以修改String值的方 ...

  3. The Go Programming Language. Notes.

    Contents Tutorial Hello, World Command-Line Arguments Finding Duplicate Lines A Web Server Loose End ...

  4. Java的String和StringBuilder

    一.String 1.创建String对象的方法: String s1="haha"; String s2=new String(); String s3=new String(& ...

  5. go语言标准库 时刻更新

    Packages   Standard library Other packages Sub-repositories Community Standard library ▾ Name Synops ...

  6. java 扫描输入

    到目前为止,从文件或标准输入读取数据还是一件相当痛苦第事情,一般第解决之道就是读入一行文本,对其进行分词,然后使用Integer Double 等类第各种解析方法来解析数据: //: strings/ ...

  7. java 格式化

    一. 可以之际像c语言一样用System.out.printf()格式化输出 二. System.out.format 1. format()方法模仿自printf(), 可用于PrintStream ...

  8. java jvm 字节码 实例

    https://blog.csdn.net/wuzhiwei549/article/details/80626677 代码 package strings; //: strings/WhitherSt ...

  9. 08 Packages 包

    Packages   Standard library Other packages Sub-repositories Community Standard library Name Synopsis ...

随机推荐

  1. Flask 模板语言,装饰器

      Jinja2模板语言 # -*- coding: utf-8 -*-   from flask import Flask, render_template, request, redirect,  ...

  2. Android Studio 3.0+ Record Espresso Test 自动化测试

    准备工作 1.将android studio 版本升级到3.0+2.百度下载夜神模拟器 夜神模拟器的基本设置 PS:以上就是夜神模拟器的基本设置 Android Studio 连接夜神模拟器 //夜神 ...

  3. LeetCode刷题191121

    博主渣渣一枚,刷刷leetcode给自己瞅瞅,大神们由更好方法还望不吝赐教.题目及解法来自于力扣(LeetCode),传送门. 数据库: 编写一个 SQL 查询,来删除 Person 表中所有重复的电 ...

  4. tensorflow 性能调优相关

    如何进行优化tensorflow 将极大得加速机器学习模型的训练的时间,下面是一下tensorflow性能调优相关的阅读链接: tensorflow 性能调优:http://d0evi1.com/te ...

  5. 开启docker

    systemctl daemon-reload systemctl restart docker.service

  6. 损失函数———有关L1和L2正则项的理解

    一.损失函: 模型的结构风险函数包括了   经验风险项  和  正则项,如下所示: 二.损失函数中的正则项 1.正则化的概念: 机器学习中都会看到损失函数之后会添加一个额外项,常用的额外项一般有2种, ...

  7. 发布Cocos2d-x的PC端程序

    发布Cocos2d-x的PC端程序 一.创建一个Release的项目 1.利用根目录下的解决方案生成Release.win32文件夹 2.新建一个cocos2d项目(比如解决方案名称MySolutio ...

  8. win7怎么使用远程桌面连接(win10类似,同样适用)

     win7使用远程桌面比mac要简单多了,只需在桌面点击“开始”,找到并打开“附件”,点击“远程桌面连接”即可   mac使用远程桌面连接:https://www.cnblogs.com/tu-071 ...

  9. LeetCode 第70题动态规划算法

    导言 看了 动态规划(https://www.cnblogs.com/fivestudy/p/11855853.html)的帖子,觉得写的很好,记录下来. 动态规划问题一直是算法面试当中的重点和难点, ...

  10. JavaScript空字符串判断

    JavaScript空字符串判断 本文完整示例代码GIT仓: 测试用例完整代码:isNullOrEmpty jPublic GIT仓:jPublic 比较常见写法 if (str == 'undefi ...