java怎么判断一个字符串中字母出现的次数
1个回答
展开全部
1、根据length获取字符串长度
1
2
String s = "abcdedfae";//定义一个字符串
int len = s.length();//获取原来的字符串长度
2、通过replaceAll方式,把字符串中该字母替换成空
1
String <a href="https://www.baidu.com/s?wd=s1&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y3PWNBrHnYnW6sPH7WPj9-0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPH6YPWDsrjD3" target="_blank"昌芦烂 class="baidu-highlight">s1</a> = s.replaceAll(要统计的字母,"");
3、获取替换后的字符哗消串长度
1
int len2 = <a href="https://www.baidu.com/s?wd=s1&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y3PWNBrHnYnW6sPH7WPj9-0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPH6YPWDsrjD3" target="_blank" class="耐漏baidu-highlight">s1</a>.length();
4、原来的字符串长度减去替换后的字符串长度就是该字母出现的次数
1
int lenTimes = len1-len2;//出现的次数
1
2
String s = "abcdedfae";//定义一个字符串
int len = s.length();//获取原来的字符串长度
2、通过replaceAll方式,把字符串中该字母替换成空
1
String <a href="https://www.baidu.com/s?wd=s1&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y3PWNBrHnYnW6sPH7WPj9-0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPH6YPWDsrjD3" target="_blank"昌芦烂 class="baidu-highlight">s1</a> = s.replaceAll(要统计的字母,"");
3、获取替换后的字符哗消串长度
1
int len2 = <a href="https://www.baidu.com/s?wd=s1&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y3PWNBrHnYnW6sPH7WPj9-0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPH6YPWDsrjD3" target="_blank" class="耐漏baidu-highlight">s1</a>.length();
4、原来的字符串长度减去替换后的字符串长度就是该字母出现的次数
1
int lenTimes = len1-len2;//出现的次数
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询