我有一个名称的字符串,其中所有不同的名称由相同的字符(#)分隔。 tom#will#robert#在java中计算字符串中字符的确切数量
我想通过计算#出现的次数来计算名称的数量,我该如何去做这件事?
我有一个名称的字符串,其中所有不同的名称由相同的字符(#)分隔。 tom#will#robert#在java中计算字符串中字符的确切数量
我想通过计算#出现的次数来计算名称的数量,我该如何去做这件事?
您需要:
yourstring.splt("#").length;
的[我如何计算一个串一个字符的出现的次数?](HTTP
...只有其他的事情是或许减去1 iff字符串不以#结尾。而且,对于非常非常长的字符串,手动计数可能更适合处理器/内存。 –
为什么要通过计算分隔符的数量来计算令牌的数量?
只需使用StringTokenizer获取所有令牌(将分隔符设置为#),然后获取其大小。
StringTokenizer(String str, String delim)
Constructs a string tokenizer for the specified string.
则:
int countTokens()
Calculates the number of times that this tokenizer's nextToken method can be called before it generates an exception.
可能重复:// stackoverflow.com/questions/275944/how-do-i-count-the-number-of-occurrences-of-a-char-in-a-string) –