publicclasstest{publicstaticvoidmain(string[]args){//指定字符和字符串字符c=“1”string=“123456789111”系统输出打印(getCharCount(c,string))}公共静态intgetCharCount(charc,stringstring){int计数=0表示(inti=0i<字符串长度()i){字符温度=字符串.charAt(i)If(C==temp)count}返回count}
1。根据字符串S1=s的长度获取字符串长度。replaceall(要计数的字母,“”)intlen2=S1。Length()intlentimes=Len1-len2//出现次数
1。根据长度获取字符串长度
strings=“abcdedfae”//定义字符串intlen=s.length()//获取原始字符串长度
2。通过replaceall
stringS1=s.replaceall(要计数的字母,”)将字符串中的字母替换为null
3。获取替换的字符串长度
intlen2=S1。长度()
4。原始字符串长度减去替换的字符串长度即为字母出现的次数
intlentimes=len1-len2//字母出现的次数