Go:字符串操作


Package strings:https://golang.google.cn/pkg/strings/

package main

import (
    "fmt"
    "strconv"
    "strings"
)

// 字符串反轉
func ReverseStr(str string) string {
    var result string
    strLen := len(str)
    for i := 0; i < strLen; i++ {
        result = result + fmt.Sprintf("%c", str[strLen-i-1])
    }
    return result
}

func main() {
    var str1 = "Hello World"
    // 測量字符串長度
    result1 := len(str1)
    fmt.Println(result1) // 11

    // 字符串反轉
    fmt.Println(ReverseStr(str1)) // dlroW olleH

    // 判斷字符串s是否以prefix開頭     // func HasPrefix(s, prefix string) bool
    fmt.Println(strings.HasPrefix(str1, "H")) // true

    // 判斷字符串s是否以suffix結尾     // func HasSuffix(s, suffix string) bool
    fmt.Println(strings.HasSuffix(str1, "ld")) // true

    // 返回字符串在s中第一個substr實例的索引,如果s中不存在substr,則返回-1
    // func Index(s, substr string) int
    fmt.Println(strings.Index("chicken", "en")) // 5
    fmt.Println(strings.Index("chicken", "gg")) // -1

    // 返回字符串在s中最后一個substr實例的索引,如果s中不存在substr,則返回-1
    //func LastIndex(s, substr string) int
    fmt.Println(strings.Index("go gopher", "go")) // 0
    fmt.Println(strings.LastIndex("go gopher", "go")) // 3
    fmt.Println(strings.LastIndex("go gopher", "rodent")) // -1

    // 字符串替換     // func Replace(s, old, new string, n int) string
    /*
    返回字符串s的副本,其中前n個非重疊的old實例替換為new。
    如果old為空,則它在字符串的開頭和每個UTF-8序列之后匹配,對k-rune字符串產生最多k + 1個替換。
    如果n <0,則替換次數沒有限制。
    */
    fmt.Println(strings.Replace("oink oink oink", "k", "pd", 2)) // oinpd oinpd oink
    fmt.Println(strings.Replace("oink oink oink", "oink", "pd", -1)) // pd pd pd

    // 計算字符串中某個字符出現次數     // func Count(s, substr string) int
    /*
    計算字符串s中非重疊substr實例的數量。
    如果substr是空字符串,則Count返回1 + s中的Unicode代碼點數。
    */
    fmt.Println(strings.Count("cheese", "e")) // 3
    fmt.Println(strings.Count("fw", "")) // 3

    // 拼接字符串本身     // func Repeat(s string, count int) string
    fmt.Println(strings.Repeat("pd", 2)) // pdpd

    // 字符串全小寫     // func ToLower(s string) string
    fmt.Println(strings.ToLower("Gopher")) // gopher

    // 字符串全小大寫     // func ToUpper(s string) string
    fmt.Println(strings.ToUpper("Gopher")) // GOPHER

    // 去掉字符串首尾空白字符     //func TrimSpace(s string) string
    fmt.Println(strings.TrimSpace(" \t\n Hello, World \n\t\r\n")) // Hello, World

    //去掉字符串首尾cutset字符     // func Trim(s string, cutset string) string
    fmt.Println(strings.Trim("¡¡¡Hello!¡World!!!", "")) // Hello!¡World

    // 去掉字符串首部cutset字符     //func TrimLeft(s string, cutset string) string
    fmt.Println(strings.TrimLeft("¡¡¡Hello!¡World!!!", "")) // Hello!¡World!!!

    // 去掉字符串尾部cutset字符     // func TrimRight(s string, cutset string) string
    fmt.Println(strings.TrimRight("¡¡¡Hello!¡World!!!", "")) // ¡¡¡Hello!¡World

    // 返回字符串空格分隔的所有子字符串片段     // func Fields(s string) []string
    fmt.Println(strings.Fields("  foo bar  baz   ")) // [foo bar baz]

    // 返回字符串split分隔的所有子串的slice     // func Split(s, sep string) []string
    fmt.Printf("%q\n", strings.Split("foo,bar,baz", ",")) // ["foo" "bar" "baz"]
    fmt.Printf("%q\n", strings.Split("a foo a bar a baz", "a ")) // ["" "foo " "bar " "baz"]
    fmt.Printf("%q\n", strings.Split(" xyz ", "")) // [" " "x" "y" "z" " "]
    fmt.Printf("%q\n", strings.Split("", "pd")) // [""]

    // 用sep把s中的所有元素連接起來,以創建單個字符串     // func Join(a []string, sep string) string
    s := []string{"foo", "bar", "baz"}
    fmt.Printf("%q\n", strings.Join(s, ", ")) // "foo, bar, baz"

    // 把一個整數i轉成字符串     //func Itoa(i int)string
    i := 10
    str2 := strconv.Itoa(i)
    fmt.Printf("%T %q\n", str2, str2) // string "10"

    // 把一個字符串轉成整數     //func Atoi(s string)(int,error)
    str3 := "10"
    if s, err := strconv.Atoi(str3); err == nil {
        fmt.Printf("%T %d\n", s, s) // int 10
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM