go中字符串类型string的⽤法
⽰例
// 字符串类型string的⽤法
package main
import (
"fmt"
"unsafe"
)
func main() {
// 字符串类型 str1
var str1 string = "上海北京 12345 西安"
fmt.Printf("str1 的值为 %s, %v, %q, 类型为 %T, 占 %d 个字节\n", str1, str1, str1, str1, unsafe.Sizeof(str1)) // str1 的值为上海北京 12345 西安, 上海北京 12345 西安, "上海北京 12345 西安", 类型为 string, 占 16 个字节
var str2, str3 string = "hello ", "world"
fmt.Printf("str2 = %q, %v, %s, str3 = %q, %v, %s\n", str2, str2, str2, str3, str3, str3) // str2 = "hello ", hello , hello , str3 = "world", world, world
// 错误:cannot assign to str2[1]
// go中的字符串是不可变的,⼀旦被赋值,不能再修改
// str2[1] = 'k'
// fmt.Printf("str2 = %v\n", str2)
// 双引号会识别转义字符
// 中⽂字符串
str4 := "你好\000北京\t上海济南武汉\007天津\x7f"
fmt.Printf("str4 = %v\n", str4) // str4 = 你好北京上海济南武汉天津
fmt.Printf("str4 = %s\n", str4) // str4 = 你好北京上海济南武汉天津
// %v和%s⼀起格式化字符串时,输出的结果不⼀样
fmt.Printf("str4 = %v, %s\n", str4, str4) // str4 = 你好北京上海济南武汉天., 你好北京上海济南武汉天津
// 英⽂字符串
str5 := "hello\000world\tgood\007yellow\x7f"
fmt.Printf("str5 = %v\n", str5) // str5 = helloworld    goodyellow
fmt.Printf("str5 = %s\n", str5) // str5 = helloworld    goodyellow
// %v和%s⼀起格式化字符串时,输出的结果不⼀样
fmt.Printf("str5 = %v, %s\n", str5, str5) // str5 = helloworld  goodyello, helloworld  goodyellow
/
/ 反引号,以字符串的原⽣形式输出,包括换⾏和特殊字符
// 下⾯⽤反引号原样输出⼀段⽰例代码
var str6 = `
// bool布尔类型的⽤法
package main
import (
"fmt"
"unsafe"
)
func main() {
// bool类型
b1 := false
fmt.Printf("b1的值为 %t, 类型为 %T, 占 %d 个字节\n", b1, b1, unsafe.Sizeof(b1)) // b1的值为 false, 类型为 bool, 占 1 个字节
// b1 = 1 会报错: cannot use 1 (type int) as type bool in assignment
// 因为b1是bool类型,只能取值true或者false
// 不可以⽤0或者⾮0的整数替代false或者true,这点与C语⾔不同
// n1 := 1
// 错误:non-bool n1 (type int) used as if condition
// if n1 {
//    fmt.Printf("n1 = %d\n", n1)
// }
// 错误:cannot use 0 (type int) as type bool in assignment
// var b2 bool = 0
// fmt.Printf("b2 = %t\n", b2)
}
`
fmt.Printf("\t⽰例代码: \n%v\n", str6)
// 以上代码会输出:go语言字符串转数组
//
// ⽰例代码:
// bool布尔类型的⽤法
// package main
// import (
/
/    "fmt"
//    "unsafe"
// )
// func main() {
//    // bool类型
//    b1 := false
//    fmt.Printf("b1的值为 %t, 类型为 %T, 占 %d 个字节\n", b1, b1, unsafe.Sizeof(b1)) // b1的值为 false, 类型为 bool, 占 1 个字节
//    // b1 = 1 会报错: cannot use 1 (type int) as type bool in assignment
//    // 因为b1是bool类型,只能取值true或者false
//    // 不可以⽤0或者⾮0的整数替代false或者true,这点与C语⾔不同
//    // n1 := 1
//    // 错误:non-bool n1 (type int) used as if condition
//    // if n1 {
//    //    fmt.Printf("n1 = %d\n", n1)
//    // }
//    // 错误:cannot use 0 (type int) as type bool in assignment
//    // var b2 bool = 0
//    // fmt.Printf("b2 = %t\n", b2)
// }
// ⽤加号 '+' 进⾏拼接
str7, str8, str9 := "你好", " 中国 ", "北京"
// 加号两边必须都是字符串才可以拼接
newStr1 := str7 + str8 + str9
fmt.Printf("拼接之后的新串为 %q, %v\n", newStr1, newStr1) // 拼接之后的新串为 "你好中国北京", 你好中国北京
// 字符串过长时⽤ '+' 进⾏分⾏,然后拼接
// 加号必须放在分⾏的最右边
var str10 string = "剑阁峥嵘⽽崔嵬,⼀夫当关,万夫莫开。" +
"所守或匪亲,化为狼与豺。朝避猛虎,⼣避长蛇;磨⽛吮⾎," +
"杀⼈如⿇。锦城虽云乐,不如早还家。蜀道之难,难于上青天," +
"侧⾝西望长咨嗟!"
fmt.Printf("str10的内容为:%v\n", str10) // str10的内容为:剑阁峥嵘⽽崔嵬,⼀夫当关,万夫莫开。所守或匪亲,化为狼与豺。朝避猛虎,⼣避长蛇;磨⽛吮⾎,杀⼈如⿇。锦城虽云乐,不如早还家。蜀道之难,难于上青天,侧⾝西望长咨嗟!}
总结

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。