golang 字符串中查找大小写字母、数字、特殊字符的个数
package main import "fmt" type StrReplaceStruct struct { CapitalLetter int `json:"capital_letter"` LowercaseLetters int `json:"lowercase_letters"` Number int `json:"number"` OtherString int `json:"other_string"` } func StrReplaceAllString(s2 string) (strReplace StrReplaceStruct) { for i := strReplace.OtherString; i < len(s2); i++ { switch { case 64 < s2[i] && s2[i] < 91: strReplace.CapitalLetter += 1 case 96 < s2[i] && s2[i] < 123: strReplace.LowercaseLetters += 1 case 47 < s2[i] && s2[i] < 58: strReplace.Number += 1 default: strReplace.OtherString += 1 } } return strReplace } func main() { str := "1234a!~`@#$*()-+=_%^&!sJKLOP" fmt.Println("字符串:", str) strs := StrReplaceAllString(str) fmt.Printf("字符串大写英文字符个数是: %d\n", strs.CapitalLetter) fmt.Printf("字符串小写英文字符个数是: %d\n", strs.LowercaseLetters) fmt.Printf("字符串数字字符个数是: %d\n", strs.Number) fmt.Printf("字符串特殊字符个数是: %d\n", strs.OtherString) }
英文下特殊字符一个符号是1位,中文下的特殊字符一个符号是3位,一个汉字占用特殊字符4位。