"GoLang" string and related operations

table of Contents

1. Introduction to Strings

  • Double quotes : the string is enclosed in double quotes, and the related escape characters will be replaced

    str := "Hello World! \n Hello Gopher! \n"
    

    Output:

    Hello World! 
    Hello Gopher!
    
  • Backticks : The string is enclosed in backticks, and the related escape characters will not be replaced

    str :=  `Hello World! \n Hello Gopher! \n`
    

    Output:

    Hello World! \nHello Gopher! \n
    

Double quotes escape character is replaced, the anti-natively quoted string \n is output as it is.

stringThe type zero value is a zero-length string, which is the empty string ""

The stringtype in Go is a value type , and the stored string is immutable . If you want to modify the stringcontentstring[]byte[]runestring , you need to convert it to or , and the modified content is redistributed .

You can use the function len()to get the length of the string in bytes

str := "asd"
len(str)

If you need to get characters, you should do this:

package main

import "fmt"

func main() {
    str := "我与春风皆过客,你携秋水揽星河。"
	for _, char := range str {
		fmt.Printf("%c", char)
	}
}

Output:

我与春风皆过客,你携秋水揽星河。

Gets the string in a byte address of behavior is illegal , for example: &str[i].

2. String concatenation

  • Use operators directly

    str := "Beginning of the string " + 
    	"second part of the string"
    

    Since the end of the line compiler automatically fill the whole reason semicolon, plus sign +must be placed on the first line.

    Shorthand splicing +=also be a string:

    s := "hel" + "lo, "
    s += "world!"
    fmt.Println(s) // 输出 “hello, world!”
    

    The strings inside are immutable, each operation will produce a new string, so it will produce a lot of temporary useless strings, not only useless, but also bring additional burden to the GC , so the performance is relatively poor .

  • fmt.Sprintf()

    str := fmt.Sprintf("%d:%s", 2018, "年")
    fmt.Println(str) // 2018:年
    

    Internal []byteimplemented, unlike direct operator will have a lot of such temporary string, but the internal logic is more complex, there are many additional judgment also uses interface, the performance in general.

  • strings.Join()

    str = strings.Join([]string{"hello", "world"}, ", ")
    fmt.Println(str) // hello, world
    

    JoinAccording to the content of the string array, the length after splicing will be calculated, and then the corresponding size of memory will be applied. One string will be filled in. In the case of an array, this efficiency will be very high, but originally there is no The cost of constructing this data is not small.

  • bytes.Buffer

    var buffer bytes.Buffer
    buffer.WriteString("hello")
    buffer.WriteString(", ")
    buffer.WriteString("world")
    
    fmt.Print(buffer.String()) // hello, world
    

    This is ideal, it can be used as a variable character used for memory optimization also increase, if the estimated length of the string, you can use buffer.Grow()the interface to setcapacity

  • strings.Builder

    var b1 strings.Builder
    b1.WriteString("ABC")
    b1.WriteString("DEF")
    
    fmt.Print(b1.String()) // ABCDEF
    

    strings.BuilderBy internal sliceto save and manage content. sliceInternally, a pointer points to the array that actually holds the content. strings.BuilderAlso it provides Grow()predefined capacity to support. When we can predefine the capacity we need to use, strings.Builderyou can avoid creating a new expansion and slicethe. strings.BuilderNon-thread-safe, and the performance bytes.Bufferis almost the same.

3. Common handling of string

There are four standard library package is particularly important for string manipulation: bytes, strings, strconvand unicodepackages.

  • stringsThe package provides a number of query string, substitutions, comparison, cut, split and merge functions
  • bytesPackage also provides many functions similar functions, but for strings and have the same structure []bytetype. Because the string is read-only, building the string gradually will result in a lot of allocation and copying. In this case, using bytes.Buffertype will be more effective
  • strconv The package provides the conversion between Boolean, integer, floating point and corresponding strings, and also provides conversions related to double-quote escaping
  • unicodePackage offers IsDigit, IsLetter, IsUpperand IsLowerand other similar functions, they are used to classify characters

3.1 strings package

3.1.1 Determine whether two UTF-8 encoded strings are the same

  • func EqualFold(s, t string) bool

The unicodeuppercase, lowercase, regarded as the same as the title character in three formats

func main() {
    str1 := "Golang"
    str2 := "golang"
    fmt.Println(strings.EqualFold(str1, str2)) // true
}

3.1.2 Determine whether the string str starts with prefix

  • strings.HasPrefix(s string,prefix string) bool
func main() {
    str := "哈利·波特"
    prefix := "哈利"
    res := strings.HasPrefix(str, prefix)
    fmt.Println(res) // true
}

3.1.3 Determine whether the string str ends with suffix

  • strings.HasSuffix(str string,suffix string) bool
func main() {
    str := "哈利·波特"
    prefix := "波特"
    res := strings.HasSuffix(str, prefix)
    fmt.Println(res) // true
}

3.1.4 Determine the position where s first appears in str, if it does not appear return -1

  • strings.Index(str string,s string) int
func main() {
    str := "哈利·波特"
    s := "波特"
    res := strings.Index(str, s)
    fmt.Println(res) // 8,这是字节的index,不是Unicode字符数组的index
}

3.1.5 Determine the position of the last occurrence of s in str, if it does not appear return -1

  • strings.LastIndex(str tring, s string) int
func main() {
    str := "哈利·波特"
    s := "·"
    res := strings.LastIndex(str, s)
    fmt.Println(res) // 6,这是字节的index,不是Unicode字符数组的index
}

3.1.6 Query the position of non-ASCII encoded characters in the parent string

  • func IndexRune(s string, r rune) int
func main() {
    str := "哈利·波特"
    s := '波'
    res := strings.IndexRune(str, s)
    fmt.Println(res) // 8,这是字节的index,不是Unicode字符数组的index
}

Although the rune character is searched, the byte index is returned.

3.1.7 String replacement

  • strings.Replace(str string, old string, newStr string, n int) string

To strthe oldstring with newStra string and returns the result of the replacement, nthe parameters for the replacement frequency n<0for infinite.

func main() {
    str := "I love her, her name is red"
    old := "her"
    newStr := "him"
    res := strings.Replace(str, old, newStr,1)
    fmt.Println(res) // I love him, her name is red
    res = strings.Replace(str, old, newStr,2)
    fmt.Println(res) // I love him, him name is red
    res = strings.Replace(str, old, newStr,-2)
    fmt.Println(res) // I love him, him name is red
}

3.1.8 Return the number of occurrences of substr in str

  • strings.Count(str string, substr string) int
func main() {
    str := "I love her, her name is red"
    substr := "e"
    count := strings.Count(str, substr)
    fmt.Println(count) // 5
}

3.1.9 Repeat count str

  • strings.Repeat(str string, count int) string
func main() {
    str := "love !"
    count := 3
    res := strings.Repeat(str, count)
    fmt.Println(res) //love!love!love!
}

3.1.10 Convert case

  • strings.ToLower(str string) string
  • strings.ToUpper(str string) string
func main() {
    str := "I Love You !"
    lower := strings.ToLower(str)
    upper := strings.ToUpper(str)
    fmt.Println(lower) // i love you !
    fmt.Println(upper) // I LOVE YOU !
}

3.1.11 Remove the closing space character

  • strings.TrimSpace(str string) string
func main() {
    str := "I Love You !      "
    res := strings.TrimSpace(str)
    fmt.Println(res) // I Love You !
}

3.1.12 Remove the cut string on both sides of str

  • strings.Trim(str string, cut string) string
func main() {
    str := "ooI Love You !oo"
    res := strings.Trim(str, "oo")
    fmt.Println(res) // I Love You !
}

3.1.13 Remove the cut string on the side of str

  • strings.TrimLeft(str string, cut string) string
  • strings.TrimRight(str string, cut string) string

3.1.14 Use spaces as separators to separate str into slices

  • strings.Fields(str string) []string
func main() {
    str := "liu hai zhang"
    res := strings.Fields(str)
    fmt.Println(res)
    for _, x := range res {
        fmt.Println(x)
    }
}

Output result:

[liu hai zhang]
liu
hai
zhang

3.1.15 Split as a delimiter, split str into slices

  • strings.Split(str string,split string) []string
func main() {
    str := "liu-hai-zhuang"
    res := strings.Split(str, "-")
    fmt.Println(res) // [liu hai zhuang]
}

3.1.16 Use sep to join all the elements in the slice into a string

  • strings.Join(slice []string,sep string) string
func main() {
	slice := []string{"liu", "hai", "zhuang"}
	res := strings.Join(slice, "-")
	fmt.Println(res) // liu-hai-zhuang
}

3.1.17 Determine whether the string s contains substring substr

  • func Contains(s, substr string) bool
func main() {
    var str = "中国,台湾"
    fmt.Println(strings.Contains(str, "台湾")) //true
    fmt.Println(strings.Contains(str, "日本")) //false
}

3.1.18 Determine whether the string s contains utf-8 code value r

  • func ContainsRune(s string, r rune) bool
func main() {
    var r rune = '中'
    var str = "中国"
    fmt.Println(strings.ContainsRune(str, r)) //true
    fmt.Println(strings.ContainsRune(str, '日')) //false
}

3.1.19 Determine whether the string s contains any character in the string chars

  • func ContainsAny(s, chars string) bool
func main() {
    var s = "我爱你,中国"
    var chars = "我与春风皆过客"
    var test  = "日"

    fmt.Println(strings.ContainsAny(s, chars)) //true
    fmt.Println(strings.ContainsAny(s, test))  //false
}

3.2 bytes packet

The method is similar to the strings package, not given for the []bytetype, omitted here, you can refer to the blog https://www.cnblogs.com/golove/p/3287729.html

3.3 strconv package

3.3.1 string to int

  • func Atoi(s string) (i int, err error)
func main() {
    numStr := "999"
    num, err := strconv.Atoi(numStr)
    if err != nil {
        fmt.Println("can't convert to int")
    } else {
        fmt.Printf("type:%T value:%#v\n", num, num) // type:int value:999
    }
}

You also can use strconvthe package:

func ParseInt(s string, base int, bitSize int) (i int64, err error)

or

func ParseUint(s string, base int, bitSize int) (n uint64, err error)

baseSpecifies hex (2-36), if baseis 0, from the character string will be pre-determined, ”0x”a 16band, ”0”a 8band, or a 10band;

bitSizeThe results must be specified integer type assignment without 0overflow, 8, 16, 32, , 64represent int, int8, int16, int32, int64;

3.3.2 int to string

  • func Itoa(i int) string
func main() {
    num := 200
    numStr := strconv.Itoa(num)
    fmt.Printf("type:%T value:%#v\n", numStr, numStr) // type:string value:"200"
}

3.3.3 string to bool

  • func ParseBool(str string) (bool, error)

When as:str , , , , , when a medium is a true value when as: , , , , , when a medium is false1tTTRUEtrueTrue
str0fFFALSEfalseFalse

func main() {
    fmt.Println(strconv.ParseBool("t"))    // true
    fmt.Println(strconv.ParseBool("TRUE")) // true
    fmt.Println(strconv.ParseBool("true")) // true
    fmt.Println(strconv.ParseBool("True")) // true
    fmt.Println(strconv.ParseBool("0"))    //false
    fmt.Println(strconv.ParseBool("f"))    //false
}

3.3.4 string to float

  • func ParseFloat(s string, bitSize int) (f float64, err error)

bitSize: 32Or 64, corresponding to the number of bits in the system

func main() {
    strF := "250.56"
    str, err := strconv.ParseFloat(strF, 64)
    if err != nil {
        fmt.Println(err)
    }
    fmt.Printf("type:%T value:%#v\n", str, str) // type:float64 value:250.56
}

3.3.5 float to string

  • func FormatFloat(f float64, fmt byte, prec, bitSize int) string

bitSizeSources indicate the type of f 32( float32: 64, float64: ), will accordingly rounding.

fmtPresentation 'f'format: -ddd.dddd( 'b'), ( -ddddp±ddd, index 'e'binary), ( -d.dddde±dddecimal 'E'index), ( -d.ddddE±dddecimal index), 'g'(the index is large with a 'e'format or 'f'formats), 'G'(the index used is large 'E'format, or 'f'format).

precControl accuracy (negative exponent parts): The 'f', , 'e', 'E'which indicates the number of digits after the decimal point; pair of 'g', 'G'that controls the total number of digits. If precis -1, it means using the minimum number, but the necessary numbers to represent f.

func main() {
    num := 250.56
    str := strconv.FormatFloat(num, 'f', 4, 64)
    fmt.Printf("type:%T value:%#v\n", str, str) // type:string value:"250.5600"
}

Of course, if the above types are converted to strings, they can be used directly fmt.Sprintf.

3.4 unicode package

3.4.1 Determine the case of characters

  • func IsUpper(r rune) bool
  • func IsLower(r rune) bool

3.4.2 Convert character case

  • func ToUpper(r rune) rune
  • func ToLower(r rune) rune

3.4.3 Judging Character Title Format

// 判断字符 r 是否为 Unicode 规定的 Title 字符
// 大部分字符的 Title 格式就是其大写格式
// 只有少数字符的 Title 格式是特殊字符
// 这里判断的就是特殊字符
func IsTitle(r rune) bool

3.4.4 Character conversion Title format

  • func ToTitle(r rune) rune

3.4.5 Convert characters to specified format

  • func To(_case int, r rune) rune

_caseUpperCaseValue: LowerCase, ,TitleCase

3.4.6 Determine whether the character is Chinese

func main() {
    for _, r := range "Hello 世界!" {
        // 判断字符是否为汉字
        if unicode.Is(unicode.Scripts["Han"], r) {
            fmt.Printf("%c", r) // 世界
        }
    }
}

For more unicode.Scriptsvalues, please refer to: http://www.cnblogs.com/golove/p/3269099.html

3.4.7 Character judgment

  • func IsDigit(r rune) bool

    IsDigitDetermine rwhether a decimal numeric characters

  • func IsNumber(r rune) bool

    IsNumberDetermine rwhether a numeric character (category N)

  • func IsLetter(r rune) bool

    IsLetterDetermine rwhether an alphabetic character (category L), kanji character is a letter

  • func IsSpace(r rune) bool

    IsSpaceDetermine rwhether a blank character, including \t, \n, \v, \f,\r

  • func IsControl(r rune) bool

    IsControlDetermine rwhether a control character

  • func IsGraphic(r rune) bool

    IsGraphicJudge character rwhether it is a "graphic character," including letters , marks , numbers , punctuation , symbols , spaces

  • func IsPrint(r rune) bool

    IsPrintDetermine whether the character ris a "printable character" defined by Go, including letters , marks , numbers , punctuation , symbols and ASCII spaces

  • func IsPunct(r rune) bool

    IsPunctDetermine rwhether a punctuation character

  • func IsSymbol(r rune) bool

    IsSymbolDetermine rwhether a sign character

Guess you like

Origin www.cnblogs.com/ice-coder/p/12725943.html