GOLANG学习13. strings使用
package main
import (
"fmt"
"strconv"
"strings"
)
//判断url 是否以http开始,没有补全
func urlprocess(url string) string {
if !strings.HasPrefix(url, "http://") {
url = fmt.Sprintf("http://%s", url)
}
return url
}
//判断path 是否以/结尾,没有补全
func pathprocess(path string) string {
if !strings.HasSuffix(path, "/") {
path = fmt.Sprintf("%s/", path)
}
return path
}
func main() {
var (
url string
path string
str string
count int
arr []string
jstr string
istr string
)
fmt.Scanf("%s%s", &url, &path)
url = urlprocess(url)
path = pathprocess(path)
//fmt.Println("url:", url)
//fmt.Println("path:", path)
//替换字符串,-1代表全部替换
str = " hello, zhongguomayun, hello, dezhonguo, woe "
str = strings.Replace(str, "g", "z", -1)
fmt.Println("str:", str)
//字符串在原串里面出现的次数
count = strings.Count(str, "o")
fmt.Println("str counts:", count)
//去掉首尾空白字符
str = strings.TrimSpace(str)
fmt.Println(str)
//去掉字符串首尾cut字符
str = strings.Trim(str, "e")
fmt.Println("Trim:", str)
//去掉字符串首cut字符
str = strings.TrimLeft(str, "h")
fmt.Println("TrimLeft:", str)
//去掉字符串尾cut字符
str = strings.TrimRight(str, "o")
fmt.Println("TrimRight:", str)
//以空格为分隔符,返回一个slice
arr = strings.Fields(str)
fmt.Println(arr)
//以一个split作为分隔符,返回一个slice
arr = strings.Split(str, ",")
fmt.Println(arr)
//以一个split把slice里面的所有元素串起来
jstr = strings.Join(arr, ";")
fmt.Println(jstr)
//数字转化成字符串
istr = strconv.Itoa(9999)
fmt.Println(istr)
//字符串转化成数字型
number, error := strconv.Atoi("88888")
if error == nil {
fmt.Println(number)
}
}