golang strings Replace 字符串替换
函数声明为:
func Replace(s, old, new string, n int) string
官方描述为:返回将s中前n个不重叠old子串都替换为new的新字符串,如果n<0会替换所有old子串。
示例代码为,每行的结果见每行上面的注释部分:
func main() {// non-overlapping: "123" repeat 6 times in ss := "123lafaldsjglad123lkfasdf123djfal123lkdjga123lksjfla123l"old := "123"new := "888"fmt.Println("non-overlapping: ")// n < 0 ,用 new 替换所有匹配上的 old;n=-1: 888lafaldsjglad888lkfasdf888djfal888lkdjga888lksjfla888lfmt.Println("n=-1: ", strings.Replace(s, old, new, -1 ))// 不替换任何匹配的 old;n=0: 123lafaldsjglad123lkfasdf123djfal123lkdjga123lksjfla123lfmt.Println("n=0: ", strings.Replace(s, old, new, 0 )) // 用 new 替换第一个匹配的 old;n=1: 888lafaldsjglad123lkfasdf123djfal123lkdjga123lksjfla123lfmt.Println("n=1: ", strings.Replace(s, old, new, 1 ))// 用 new 替换前 5 个匹配的 old(实际多于 5 个);n=5: 888lafaldsjglad888lkfasdf888djfal888lkdjga888lksjfla123lfmt.Println("n=5: ", strings.Replace(s, old, new, 5 )) // 用 new 替换前 7 个匹配上的 old(实际没那么多);n=7: 888lafaldsjglad888lkfasdf888djfal888lkdjga888lksjfla888lfmt.Println("n=7: ", strings.Replace(s, old, new, 7 )) // overlapping:s = "888888888888888888"old = "888"new = "666"fmt.Println("overlapping: ")// n < 0 ,用 new 替换所有匹配上的 old;n=-1: 666666666666666666fmt.Println("n=-1: ", strings.Replace(s, old, new, -1 )) // 不替换任何匹配的 old;n=0: 888888888888888888fmt.Println("n=0: ", strings.Replace(s, old, new, 0 )) // 用 new 替换第一个匹配的 old;n=1: 666888888888888888fmt.Println("n=1: ", strings.Replace(s, old, new, 1 )) // 用 new 替换前 5 个匹配的 old(实际多于 5 个);n=5: 666666666666666888fmt.Println("n=5: ", strings.Replace(s, old, new, 5 ))// 用 new 替换前 7 个匹配上的 old(实际没那么多);n=7: 666666666666666666fmt.Println("n=7: ", strings.Replace(s, old, new, 7 ))
}
golang strings Replace 字符串替换相关推荐
- python3.6 str.replace() 字符串替换方法
python3.6 str.replace() 字符串替换方法 源码 def replace(self, old, new, count=None): # real signature unknown ...
- 5. 字符串替换问题(golang)
func replaceBlank(s string) (string, bool) {if len(s) > 1000 {return s, false}for _, v := range s ...
- mysql 字符串替换:处理历史稿件中的图片路径
REPLACE字符串替换函数 REPLACE(str,from_str,to_str) 示例: select REPLACE('123abcd89', 'abcd', '4567'); 输出结果: 1 ...
- golang中的strings.Replace
golang中的字符串操作strings.Replace package mainimport ("fmt""strings")//golang字符串操作 fu ...
- Go 学习笔记(51)— Go 标准库之 strings(字符串比较、字符串前后缀、字符串统计、字符串索引、字符串包含、字符串转换、字符串复制、字符串替换、字符串去除、字符串分割和连接)
1. 概述说明 import "strings" strings 包实现了用于操作字符的简单函数. strings 包与 bytes 包中的函数用法基本一样. 2. 主要函数 2. ...
- C#不区分大小写的字符串替换(Replace)函数
在.NET中,不调用C++/CLI,进行字符串替换有好几种方法: 1.最常用的,就是String实例.Replace(),但这个不能忽略大小写. 2.System.Text.Regex(Regular ...
- python字符串替换replace,Python字符串替换
Python字符串替换 Python字符串替换教程 在 Python replace()函数详解 定义 replace() 方法把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第 ...
- python replace函数用法_Python实现的从右到左字符串替换方法示例
本文实例讲述了Python实现的从右到左字符串替换方法.分享给大家供大家参考,具体如下: 一 . 前言 需要用到,但是发现python没有从右边开始替换的内置方法,默认的replace只是从左边开始, ...
- python3 字符串替换 replace translate re.sub
Python3的字符串替换,这里总结了三个函数,replace()和translate()和re.sub() replace() python 中的 replace() 方法把字符串中的 old(旧字 ...
最新文章
- SharePoint 2010中的客户端模型
- linux脚本查看变量类型,Shell变量:Shell变量的定义、删除变量、只读变量、变量类型...
- LA3602DNA序列
- sap中Excel的模版上传和下载
- VTK:IO之ReadExodusData
- IOS选中按钮状态的记录
- [剑指offer][JAVA]面试题第[10-2]题[青蛙跳台阶问题][动态规划][递归]
- 【服务器】创建docker、运行jupyter相关命令
- 手把手教你做iOS的soap应用(webservice)
- Java修改带回显吗_修改的是根据自定义标签进行处理回显
- jQuery实现留言板案例
- AE遮罩路径扭曲插件BOA Boa for Macv1.3.1破解版
- PCI Express 16X和PCIE 2.0有什么区别?哪个更好?
- 开源软件的版权与许可证
- kali 基于虚拟机下载
- Python去除文本所有标点符号
- ROS——发布摄像头节点并编写opencv图像处理节点(python)
- AI工具是帮手还是助手:
- 50万VPN登录凭证遭泄露,中国占11.89%
- 玩转儿童语音数据,打造儿童专属AI