目录

【Golang】字符串的查找方法小结

警告
本文最后更新于 2020-10-31,文中内容可能已过时。

Contains 函数

  1. func Contains(s, substr string) bool

    用途:查找字符串 substr 是否在字符串 s 内,存在就返回 true

    • 精确匹配

    • 区分大小写

    • 字符串必须相连,无法个几个再匹配

    • 空字符串也是匹配项

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    
    package main
    
    import (
    	"fmt"
    	"strings"
    )
    
    func main() {
    	fmt.Println(strings.Contains("aomineKing", "ao")) //true
    	fmt.Println(strings.Contains("aomineKing", "ek")) //false
    	fmt.Println(strings.Contains("aomineKing", "aK")) //false
    	fmt.Println(strings.Contains("aomineKing", ""))   //true
    	fmt.Println(strings.Contains("", ""))             //true
    }
    
  2. func ContainsAny(s, chars string) bool

    用途:查找 chars 中任意个字符在 s 中,若存在则返回 true

    • 模糊匹配,只要是一个字符存在,则 true
    • 空字符串不匹配
    • 区分大小写
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    
    package main
    
    import (
    	"fmt"
    	"strings"
    )
    
    func main() {
    	fmt.Println(strings.ContainsAny("aomineKing", "k"))    //false
    	fmt.Println(strings.ContainsAny("aomineKing", "ui"))   //true
    	fmt.Println(strings.ContainsAny("aomineKing", "qwe"))  //true
    	fmt.Println(strings.ContainsAny("aomineKing", "uaoK")) //true
    	fmt.Println(strings.ContainsAny("aomineKing", ""))     //false
    	fmt.Println(strings.ContainsAny("", ""))               //false
    }
    

    参考:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    
    package main
    
    import (
    	"fmt"
    	"strings"
    )
    
    var (
    	coins = 50
    	users = []string{
    		"Matthew", "Sarah", "Augustus", "Heidi", "Emilie", "Peter", "Giana", "Adriano", "Aaron", "Elizabeth",
    	}
    	distribution = make(map[string]int, len(users))
    	rewardRules  = map[string]int{
    		"e": 1,
    		"i": 2,
    		"o": 3,
    		"u": 4,
    	}
    )
    
    func dispatchCoin() int {
    	var leftCoins int
    	for _, name := range users {
    		tempCount := 0
    		for k, v := range rewardRules {
    			tempCount += strings.Count(strings.ToLower(name), k) * v
    		}
    		distribution[name] = tempCount
    	}
    	for k, v := range distribution {
    		fmt.Printf("【%v】分到多少枚【%d】金币\n", k, v)
    		leftCoins += v
    	}
    	return coins - leftCoins
    }
    
    func main() {
    	left := dispatchCoin()
    	fmt.Println("剩下:", left)
    
    }
    

    结果:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    
    Sarah分到多少枚0金币
    Heidi分到多少枚5金币
    Giana分到多少枚2金币
    Adriano分到多少枚5金币
    Aaron分到多少枚3金币
    Elizabeth分到多少枚4金币
    Matthew分到多少枚1金币
    Augustus分到多少枚12金币
    Emilie分到多少枚6金币
    Peter分到多少枚2金币
    剩下 10
    

参考:

https://www.godoc.org/strings#Contains