zoukankan      html  css  js  c++  java
  • go 字符串操作

     1 package main
     2 
     3 import (
     4    "fmt"
     5    "strconv"
     6    "strings"
     7 )
     8 
     9 // 字符串遍历
    10 func bL(str string) {
    11    fmt.Println(len(str)) // 字节长度
    12    // for range 遍历
    13    for i, ch := range str{
    14       fmt.Printf("%d:%c", i, ch)
    15    }
    16 
    17    fmt.Println("--------------")
    18    for i, ch := range []byte(str){
    19       fmt.Println(i, ch)
    20    }
    21 
    22    fmt.Println("-------------")
    23    for i, ch := range []rune(str){
    24       fmt.Println(i, ch)
    25    }
    26 
    27 }
    28 
    29 // strings包
    30 func testStrings()  {
    31    s := "我爱go语言"
    32    fmt.Println(strings.Contains(s,"我爱"))
    33    fmt.Println(strings.HasPrefix(s,"我爱"))
    34    fmt.Println(strings.HasSuffix(s,"go语言"))
    35    fmt.Println(strings.Count(s, ""))
    36    fmt.Println(strings.Index(s, ""))
    37 
    38    s1 := "abc def ggg 我"
    39    fmt.Println(strings.Fields(s1)) // 按空格分割
    40    fmt.Println(strings.Split(s, "")) // 按指定字符分割
    41 
    42    s2 := "my home isjiangxi"
    43    fmt.Println(strings.Title(s2)) // 单词首字母大写
    44    fmt.Println(strings.ToLower(s2))
    45    fmt.Println(strings.ToUpper(s2))
    46 
    47    fmt.Println(strings.Trim(s2, "mix"))  // 去掉两端所有的m i x 字符
    48    fmt.Println(strings.TrimSpace(s2)) // 去掉两段空白符
    49 
    50    fmt.Println(strings.Join([]string{"a", "b", "c"}, ":"))
    51 }
    52 
    53 func main()  {
    54    if age, err := strconv.Atoi("10"); err == nil{
    55       fmt.Println(age)
    56    }
    57    if age, err := strconv.ParseInt("20", 10, 64); err == nil{
    58       fmt.Println(age)
    59    }
    60    if age, err := strconv.ParseBool("f"); err == nil{
    61       fmt.Println(age)
    62    }
    63    if age, err := strconv.ParseFloat("10.11", 64); err == nil{
    64       fmt.Println(age)
    65    }
    66 
    67    fmt.Println(strconv.Itoa(10))
    68 }
  • 相关阅读:
    UVA
    UVA
    模板——扩展欧几里得算法(求ax+by=gcd的解)
    UVA
    模板——2.2 素数筛选和合数分解
    模板——素数筛选
    Educational Codeforces Round 46 (Rated for Div. 2)
    Educational Codeforces Round 46 (Rated for Div. 2) E. We Need More Bosses
    Educational Codeforces Round 46 (Rated for Div. 2) D. Yet Another Problem On a Subsequence
    Educational Codeforces Round 46 (Rated for Div. 2) C. Covered Points Count
  • 原文地址:https://www.cnblogs.com/zhangjian0092/p/12521109.html
Copyright © 2011-2022 走看看