备忘录吕吕没有备忘录十新建_去弦备忘单

备忘录吕吕没有备忘录十新建

检查字符串是否以子字符串开头 (Check if a string starts with a substring)

package main

import (
    "strings"
)

func main() {
    strings.HasPrefix("flavio", "fla") // true
}

play

检查字符串是否以子字符串结尾 (Check if a string ends with a substring)

package main

import (
    "strings"
)

func main() {
    strings.HasSuffix("flavio", "vio") // true
}

play

计算字符串切片中的最大字符串长度 (Calculate the maximum string length in a slice of strings)

// calculatemaxwidth given a slice of strings calculates the maximum
// length
func calculatemaxwidth(lines []string) int {
    w := 0
    for _, l := range lines {
        len := utf8.RuneCountInString(l)
        if len > w {
            w = len
        }
    }

    return w
}

比较不区分大小写的字符串 (Comparing strings case insensitive)

Instead of running ToUpper() or ToLower() from the strings or bytes packages, use strings.EqualFold() or bytes.EqualFold(), because they are guaranteed to work across all languages.

不要从stringsbytes包中运行ToUpper()ToLower() ,而是使用strings.EqualFold()bytes.EqualFold() ,因为可以保证它们可以在所有语言中使用。

package main

import (
	"bytes"
	"fmt"
)

func main() {
	fmt.Println(bytes.EqualFold([]byte("Go"), []byte("go")))
}

翻译自: https://flaviocopes.com/golang-cheat-sheet-strings/

备忘录吕吕没有备忘录十新建

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值