Go基础编程实践(一)—— 操作字符串
修剪空格
strings包中的TrimSpace函数用于去掉字符串首尾的空格。
package main
import (
"fmt"
"strings"
)
func main() {
helloWorld := "\t Hello, World "
trimHello := strings.TrimSpace(helloWorld)
fmt.Printf("%d %s\n", len(helloWorld), helloWorld)
fmt.Printf("%d %s\n", len(trimHello), trimHello)
// 15 Hello, World
// 12 Hello, World
}
提取子串
Go字符串的底层是read-only的[]byte,所以对切片的任何操作都可以应用到字符串。
package main
import "fmt"
func main() {
helloWorld := "Hello, World and Water"
cutHello := helloWorld[:12]
fmt.Println(cutHello)
// Hello, World
}
替换子串
strings包的Replace函数可以对字符串中的子串进行替换。
package main
import (
"fmt"
"strings"
)
func main() {
helloWorld := "Hello, World. I'm still fine."
replaceHello := strings.Replace(helloWorld, "fine", "OK", 1)
fmt.Println(replaceHello)
// Hello, World. I'm still OK.
}
// 注:Replace函数的最后一个参数表示替换子串的个数,为负则全部替换。
转义字符
字符串中需要出现的特殊字符要用转义字符\转义先,例如\t需要写成\\t。
package main
import "fmt"
func main() {
helloWorld := "Hello, \t World."
escapeHello := "hello, \\t World."
fmt.Println(helloWorld)
fmt.Println(escapeHello)
// Hello, World.
// Hello, \t World.
}
大写字符
strings包的Title函数用于将每个单词的首字母大写,ToUpper函数则将单词的每个字母都大写。
package main
import (
"fmt"
"strings"
)
func main() {
helloWorld := "hello, world. i'm still fine."
titleHello :=strings.Title(helloWorld)
upperHello := strings.ToUpper(helloWorld)
fmt.Println(titleHello)
fmt.Println(upperHello)
// Hello, World. I'M Still Fine.
// HELLO, WORLD. I'M STILL FINE.
}
Go基础编程实践(一)—— 操作字符串的更多相关文章
- k2datas 基础编程题,判断字符串是否有重复串
package String; public class DuplicateString { public static boolean isDup(String s) throws Exceptio ...
- Go基础编程实践(三)—— 日期和时间
日期和时间 package main import ( "fmt" "time" ) func main() { // 获取当前时间 current := ti ...
- Go基础编程实践(十)—— 数据库
从数据库中读取数据 在http://sqlitebrowser.org/下载sqlite3可视化工具,在本main.go同目录下创建personal.db数据库,创建表如下: package main ...
- Go基础编程实践(九)—— 网络编程
下载网页 package main import ( "io/ioutil" "net/http" "fmt" ) func main() ...
- Go基础编程实践(八)—— 系统编程
捕捉信号 // 运行此程序,控制台将打印"Waiting for signal" // 按Ctrl + C 发送信号以关闭程序,将发生中断 // 随后控制台依次打印"Si ...
- Go基础编程实践(七)—— 并发
同时运行多个函数 观察常规代码和并发代码的输出顺序. // 常规代码,顺序执行,依次输出 package main import ( "fmt" "time" ...
- Go基础编程实践(六)—— 文件
检查文件是否存在 在此程序同目录下创建log.txt文件,以检测. package main import ( "os" "fmt" ) func main() ...
- Go基础编程实践(五)—— 错误和日志
自定义错误类型 Go中可以使用errors.New()创建错误信息,也可以通过创建自定义错误类型来满足需求.error是一个接口类型,所有实现该接口的类型都可以当作一个错误类型. // error类型 ...
- Go基础编程实践(四)—— 数组和map
数组去重 package main import "fmt" func main(){ intSlice := []int{1,5,5,5,5,7,8,6,6, 6} fmt.Pr ...
随机推荐
- linux 所有命令无法使用
配置nginx时,错误export之后linux 所有命令无法使用 出现这个问题是因为系统的环境变量没有正确配置造成的,造成这个原因有很多,比如系统升级,比如不正当操作等导致环境变量被覆盖修改,解决的 ...
- nginx 反向代理之 proxy_cache
proxy_cache将从C上获取到的数据根据预设规则存放到B上(内存+磁盘)留着备用,A请求B时,B会把缓存的这些数据直接给A,而不需要再去向C去获取. proxy_cache相关功能生效的前提是, ...
- 编译错误: file not found with angled include use quotes instead #include <lualib.h> 和 #include "lualib.h"
http://stackoverflow.com/questions/17465902/use-of-external-c-headers-in-objective-c 问题: 7down votef ...
- java并发编程(一)线程状态 & 线程中断 & 线程间的协作
参考文章: Java线程的5种状态及切换:http://blog.csdn.net/pange1991/article/details/53860651 线程的5种状态: 1. 新建(NEW):新创建 ...
- JPA的懒加载
JPA数据懒加载LAZY和实时加载EAGER(二) 懒加载LAZY和实时加载EAGER的概念,在各种开发语言中都有广泛应用.其目的是实现关联数据的选择性加载,懒加载是在属性被引用时,才生成查询语句 ...
- Golang常见小细节总结(1)
本系列不定期更新,用于记录平常开发过程中出现的一些小问题 Array 类型的值作为函数参数    可以理解slice是对array的一个视图,底层还是array所以会被修改 通过map的ok来确 ...
- LeetCode 101. Symmetric Tree(镜像树)
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- GWAS+自然选择:62个样本的GWAS分析,没信号,如何巧妙的发文章
欢迎来到"bio生物信息"的世界 6天前,BMC Genomics 推了一篇文献"Population history and genetic adaptation of ...
- [Golang] Gin框架学习笔记
0x0 Gin简介 1.Gin 是什么? Gin 是一个用 Go (Golang) 编写的 HTTP web 框架. 它是一个类似于 martini 但拥有更好性能的 API 框架, 由于 httpr ...
- 'object ''/usr/local/lib/libdns.so'' from /etc/ld.so.preload cannot be preloaded: ignored.'
做了如下操作后: rm -rf xxx.jar kill -9 xx 重启Jar包,出现如下错误: ld.so: object '/usr/local/lib/libdns.so' from /etc ...