package main
import "fmt"
func testCalc(str string) (charCount, spaceCount, numberCount, otherCount int) {
utfChars := []rune(str)
for i := 0; i < len(utfChars); i++ {
if (utfChars[i] >= 'a' && utfChars[i] <= 'z') || (utfChars[i] >= 'a' && utfChars[i] <= 'z') {
charCount++
continue
}
if utfChars[i] >= '0' && utfChars[i] <= '9' {
numberCount++
continue
}
if utfChars[i] == ' ' {
spaceCount++
continue
}
otherCount++
}
return
}
func testExample() {
str := "dssd 世界 22"
charCount,numCount,spCount,other := testCalc(str)
fmt.Printf("charCount =%d,numCount = %d,spCount =%d,other = %d",charCount,numCount,spCount,other)
}
func main() {
testExample()
}
输出结果:
charCount =4,numCount = 2,spCount =2,other = 2