s1 := "hello"
s2 := "您好"
package main
import (
"fmt"
)
func main() {
fmt.PrintIn("str := \"c:\\Code\\demo\\go.exe\"") // str := "c:\Code\demo\go.exe"
}
package main
import (
"fmt"
)
func main() {
s1 := `
第一行
第二行
第三行`
fmt.Println(s1)
}
uint8 类型,或者叫 byte 型:代表了 ASCII 码的一个字符
rune 类型:代表一个 UTF-8 字符
package main
import "fmt"
func main(){
//"比赛第一"
s := "比赛第一"
s_rune := []rune(s)
fmt.PrintIn("友谊"+string(s_rune[2:])) //友谊第一
}
方法 | 介绍 |
len(str) | 求长度 |
+或 fmt.Sprintf | 拼接字符串 |
strings.Split | 分割 |
strings.Contains | 判断是否包含 |
strings.HasPrefix,strings.HasSuffix | 前缀/后缀判断 |
strings.Index(),strings.LastIndex() | 子串出现的位置 |
strings.Join(a[]string,sep string) | join 操作 |
package main
import "fmt"
func main(){
var str = "this is str"
fmt.PrintIn(len(str)) //11
}
package main
import "fmt"
func main() {
var str1 = "您好"
var str2 = "golang"
fmt.PrintIn(str1 + str2) //你好golang
}
package main
import (
"fmt"
"strings"
)
func main () {
var s = "Running-Snail"
var s1 = strings.Split(s,"-")
fmt.PrintIn(s1) // [Running Snail]
}
package main
import (
"fmt"
"strings"
)
func main() {
//1.判断字符串 以 this 开头
var str = "this is golang"
var flag = strings.Hasprefix(str,"this")
fmt.PrintIn(flag) //true
//2.判断字符串以 go 结尾
var.flag2 = strings.HasSuffix(str,"go")
fmt.PrintIn(flag2) //false
}
package main
import (
"fmt"
"strings"
)
func main() {
var str = "this is golang"
var index =strings.Index(str,"go")
fmt.PrintIn(index) // 8 (判断字符串 go 出现的位置)
}
package main
import (
"fmt"
"strings"
)
func main() {
var str = "Running-Snail"
var arr = strings.Split(str,"-") // [Running Snail]
var str2 =strings.Join(arr,"*") // Running*Snail
fmt.PrintIn(arr)
fmt.PrintIn(str2)
}
package main
import "fmt"
func main(){
a := 'a'
name := "snail"
//当我们直接输出 byte(字符)的时候输出的是这个字符对应的码值
fmt.PrintIn(a) // 97 这里输出的是 a 字符串的 ASCII 值
fmt.PrintIn(name) // snail
//如果我们要输出这个字符,需要格式化输出
fmt.Printf("的值是%c",a) //a的值是 a
}
package main
import "fmt"
func main(){
s := "hello snail"
for i := 0; i%c",r,r)
//104=>h 101=>e 108=>l 108=>l 111=>o 32=> 115=>s 110=>n 97=>a 105=>i 108=>l
}
}
package main
import "fmt"
func main(){
s1 := "snail"
//强制类型转换
byteS := []byte(s1)
byteS1[0] = 'p'
fmt.PrintIn(string(byteS1)) //pnail
s2 := "白萝卜"
runeS2 := []rune(s2)
runeS2[0] = '红'
fmt.PrintIn(string(runeS2)) //红萝卜
}
package main
import "fmt"
func main() {
var i int = 20
var f float64 = 12.456
var t bool = true
var b byte = 'a'
var strs string
strs=fmt.Sprintf("%d",i) //把int 转换成 string
fmt.Printf("类型: %T,值=%v\n",strs,strs) //类型:string,值 =20
strs= fmt.Sprintf("%f",f) //把 float转换成 string
fmt.Printf("类型: %T ,值=%v \n", strs, strs) // 类型: string ,值=12.456000
strs = fmt.Sprintf("%t", t) // 把 bool 转换成 string
fmt.Printf("类型: %T ,值=%v \n", strs, strs) // 类型: string ,值=true
strs = fmt.Sprintf("%c", b) // 把 byte 转换成 string
fmt.Printf("类型: %T ,值=%v \n", strs, strs) // 类型: string ,值=a
}
package main
import (
"fmt"
"strconv"
)
fun main () {
//1、int转换成 string
var num1 int = 20
s1 := strong.Itoa(num1)
fmt.Printf("类型:%T ,值=%v \n",s1,s1) //类型:string,值=20
//2、float 转 string
var num2 float64 = 20.113123
/*参数 1:要转换的值
参数 2:格式化类型
参数 3:保留的小数点 -1 (不对小数点格式化)
参数 4:格式化的类型
*/
s2 := strconv.FormatFloat(num2,'f',2,64)
fmt.Printf("类型: %T ,值=%v \n", s2, s2) // 类型: string ,值=20.11
// 3、bool 转 string
s3 := strconv.FormatBool(true)
fmt.Printf("类型: %T ,值=%v \n", s3, s3) // 类型: string ,值=20.11
//4、int64 转 string
var num3 int64 = 20
s4 := strconv.FormatInt(num3, 10) /* 第二个参数10为 进制 */
fmt.Printf("类型 %T ,值=%v \n", s4, s4) // 类型 string ,值=20
}
package main
import (
"fmt"
"strconv"
)
fun main() {
var s = "1234"
i64,_ := strconv.ParseInt(s,10,64)
fmt.Printf("值:%v 类型:%T",i64,i64) //值:1234 类型:int64
}
package main
import (
"fmt"
"strconv"
)
func main() {
str := "3.1415926535"
v1,_ := strconv.ParseFloat(str,32)
v2,_ := strconv.ParseFloat(str,64)
fmt.Printf("值:%v 类型:%T\n",v1,v1) // 值:3.1415927410125732 类型:float64
fmt.Printf("值:%v 类型:%T", v2, v2) // 值:3.1415926535 类型:float64
}
package main
import (
"fmt"
"strconv"
)
func main() {
b, _ := strconv.ParseBool("true") // string 转 bool
fmt.Printf("值:%v 类型:%T", b, b) // 值:true 类型:bool
}
package main
import "fmt"
func main() {
s := "hello snail"
for _,r := range s{ //rune
//104(h) 101(e) 108(l) 108(l) 111(o) 32( ) 115(s) 110(n) 97(a) 105(i) 108(l)
fmt.Printf("%v(%c)",r,r)
}
fmt.PrintIn()
}
package main
func Reverse(s string) string{
r := []rune(s)
for I,j:=0,len(r)-1;i