字符串函数
package main
import(
"fmt"
"strconv"
"strings"
)
func main(){
str1 := "李威彤"
str2 := []rune(str1)
for i:=0;i<len(str2);i++{
fmt.Printf("%c\n",str2[i])
}
n,err := strconv.Atoi("123")
if err != nil{
fmt.Println("出错了:",err)
}else{
fmt.Println("转成的结果为:",n)
}
str3 := strconv.Itoa(12345)
fmt.Printf("%v,%T\n",str3,str3)
var bytes = []byte("lwt666")
fmt.Printf("%v\n",bytes)
str4 := string([]byte{97,98,99})
fmt.Printf("%v\n",str4)
str5:=strconv.FormatInt(8,2)
fmt.Printf("8的二进制%v\n",str5)
str6:=strconv.FormatInt(8,16)
fmt.Printf("8的十六进制%v\n",str6)
bool1:=strings.Contains("lwt666","lwt")
fmt.Println(bool1)
num1:=strings.Count("lwwt","w")
fmt.Println(num1)
bool2 := strings.EqualFold("lwt","lwT1")
fmt.Println(bool2)
index1 := strings.Index("ggo","go")
fmt.Println(index1)
index2 := strings.LastIndex("ggo go","go")
fmt.Println(index2)
str7 := strings.Replace("go go 666","go","lwt",1)
fmt.Println("str7=",str7)
str8 := strings.Split("hello,lwt,666",",")
for i:=0; i<len(str8);i++{
fmt.Println(str8[i])
}
fmt.Println(str8)
str9 := "Go Lang"
str9 = strings.ToLower(str9)
fmt.Println(str9)
str10 := "Go Lang"
str10 = strings.ToUpper(str10)
fmt.Println(str10)
str11 := " !lwt! "
str11 = strings.Trim(str11," !")
fmt.Println(str11)
bool3 := strings.HasPrefix("http://www.baidu.com","http");
fmt.Println(bool3)
}
日期函数与格式化
package main
import(
"fmt"
"time"
)
func main(){
timeStr := time.Now()
fmt.Println(timeStr)
fmt.Printf("%d-%d-%d %d:%d:%d\n",timeStr.Year(),timeStr.Month(),timeStr.Day(),
timeStr.Hour(),timeStr.Minute(),timeStr.Second())
time1 := timeStr.Format("2006-01-02 15:04:05")
fmt.Println(time1)
time2 := timeStr.Format("2006-01-02")
fmt.Println(time2)
time3 := timeStr.Format("15:04:05")
fmt.Println(time3)
fmt.Println(timeStr.Unix())
fmt.Println(timeStr.UnixNano())
}
测试程序执行时间
package main
import(
"fmt"
"strconv"
"time"
)
func main(){
start := time.Now().Unix()
test01()
end := time.Now().Unix()
fmt.Println(end-start)
}
func test01(){
str := ""
for i:=0;i<100000;i++{
str+="hello"+strconv.Itoa(i)
}
}