這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
這個函數主要統計字母個數、數字個數、漢字和其他字元的個數(注意漢字和其他字元一起統計)
GO語言的代碼有
func main() { searchCount("Golang python") searchCount("我哼著" + "12345,54321" + "不小心踩了一坨屎,It smells good")}func searchCount(src string) { letters := "abcdefghijklmnopqrstuvwxyz" letters = letters + strings.ToUpper(letters) nums := "0123456789" numCount := 0 letterCount := 0 othersCount := 0 for _, i := range src { switch { case strings.ContainsRune(letters, i) == true: letterCount += 1 case strings.ContainsRune(nums, i) == true: numCount += 1 default: othersCount += 1 } } fmt.Println(letterCount, numCount, othersCount)}
python代碼簡潔了一點
def searchCount(src): numCount=0 letterCount=0 otherCount=0 for i in src: if i.isdigit(): numCount+=1 elif i.isalpha(): letterCount+=1 else: otherCount+=1 print(letterCount,numCount,otherCount)searchCount("Golang python")a="我哼著" + "12345,54321" + "不小心踩了一坨屎,It smells good"searchCount(a)