个性化阅读
专注于IT技术分析

Go使用字符串

Go字符串是可变宽度字符的序列。

Go字符串和文本文件占用更少的内存或磁盘空间。由于UTF-8是标准, 所以Go不需要编码和解码字符串。

Go字符串是值类型, 并且是不可变的。这意味着, 如果你创建一个字符串, 则无法修改该字符串的内容。字符串的初始值默认为空“”。

转到字符串示例

package main
import ("fmt"
      "reflect"
)
func main()  {
   var x string = "Hello World"
   fmt.Println(x)
   fmt.Println(reflect.TypeOf(x))
}

输出:

Hello World
string

仅转到字符串()示例

package main
import "fmt"
func main() {
   str := "I love my country"
   fmt.Println(len(str))
}

输出:

17

转到打印ASCII示例

package main
import "fmt"
func main() {
   fmt.Println("Ascii value of A is ", "A"[0])
}

输出:

Ascii value of A is  65

Go String ToUpper()示例

package main
import "fmt"
import "strings"
func main() {
   str := "india"
   fmt.Println(strings.ToUpper(str))
}

输出:

INDIA

Go String ToLower()示例

package main
import "fmt"
import "strings"
func main() {
   str := "INDIA"
   fmt.Println(strings.ToLower(str))
}

输出:

india

转到字符串HasPrefix()示例

package main
import "fmt"
import "strings"
func main() {
   s := "INDIA"
   fmt.Println(strings.HasPrefix(s, "IN"))
}

输出:

true

转到字符串HasSuffix()示例

package main
import "fmt"
import "strings"
func main() {
   s := "INDIA"
   fmt.Println(strings.HasSuffix(s, "IA"))
}

输出:

true

Go String Join()示例

package main
import "fmt"
import "strings"
func main() {
   var arr = []string{"a", "b", "c", "d"}
   fmt.Println(strings.Join(arr, "*"))
}

输出:

a*b*c*d

转到String Repeat()示例

package main
import "fmt"
import "strings"
func main() {
   var str = "New "
   fmt.Println(strings.Repeat(str, 4))
}

输出:

New New New New

转到String Contains()示例

package main
import "fmt"
import "strings"
func main() {
   str:= "Hi...there"
   fmt.Println(strings.Contains(str, "th"))
}

输出:

true

转到String Index()示例

package main
import "fmt"
import "strings"
func main() {
   str:= "Hi...there"
   fmt.Println(strings.Index(str, "th"))
}

输出:

5

Go String Count()示例

package main
import "fmt"
import "strings"
func main() {
   str:= "Hi...there"
   fmt.Println(strings.Count(str, "e"))
}

输出:

2

转到String Replace()示例

package main
import "fmt"
import "strings"
func main() {
   str:= "Hi...there"
   fmt.Println(strings.Replace(str, "e", "Z", 2))
}

输出:

Hi...thZrZ

转到String Split()示例

package main
import "fmt"
import "strings"
func main() {
   str := "I, love, my, country"
   var arr []string = strings.Split(str, ", ")
   fmt.Println(len(arr))
   for i, v := range arr {
      fmt.Println("Index : ", i, "value : ", v)
   }
}

输出:

4
Index :  0 value :  I
Index :  1 value :  love
Index :  2 value :  my
Index :  3 value :  country

Go String Split()示例2

package main
import (
   "fmt"
   "strings"
)
func main() {
   fmt.Printf("%q\n", strings.Split("x, y, z", ", "))
   fmt.Printf("%q\n", strings.Split(" John and Jack and Johnny and Jinn ", "and"))
   fmt.Printf("%q\n", strings.Split(" abc ", ""))
   fmt.Printf("%q\n", strings.Split("", "Hello"))
}

输出:

["x" "y" "z"]
[" John " " Jack " " Johnny " " Jinn"]
[" " "a" "b" "c" " "]
[""]

转到String Compare()示例

package main
import (
   "fmt"
   "strings"
)
func main() {
   fmt.Println(strings.Compare("a", "b"))
   fmt.Println(strings.Compare("a", "a"))
   fmt.Println(strings.Compare("b", "a"))
}

输出:

-1
0
1

Go String Trim()示例

package main
import (
   "fmt"
   "strings"
)
func main() {
      fmt.Println(strings.TrimSpace(" \t\n I love my country  \n\t\r\n"))
}

输出:

I love my country

转到String ContainsAny()示例

package main
import (
   "fmt"
   "strings"
)
func main() {
   fmt.Println(strings.ContainsAny("Hello", "A"))
   fmt.Println(strings.ContainsAny("Hello", "o & e"))
   fmt.Println(strings.ContainsAny("Hello", ""))
   fmt.Println(strings.ContainsAny("", ""))
}

输出:

false
true
false
false
赞(0)
未经允许不得转载:srcmini » Go使用字符串

评论 抢沙发

评论前必须登录!