[英]How do you find the amount of times a single character appears in a string and then store it into another string?
这是我现在拥有的代码:
String dog = "my!cat!is!brown!so!what!";
String temp = "";
for(int i = 0; i <= dog.length(); i++) {
if(dog.charAt(i) == '!') {
temp+= "a";
}
}
我正在尝试将其打印到可以打印“ 6”的位置,并且我不知道我是否朝错误的方向前进。 不要求代码只是提示谢谢。
我会尝试在您的for循环之前创建一个计数器。 然后,在每次要查找的字符出现时,增加计数器。
int j = 0; //Don't use i since it is your loop counter
//Each occurence:
j++;
然后在循环完成时将该int计数器转换为您的字符串对象。
int count = StringUtils.countOccurrencesOf(dog, dog[i]);
if(temp.indexOf(dog[i])==-1)
{
temp+=dog[i]+": "+count+",";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.